Loading Text From XML
<strings> <string id="someString"> <![CDATA[Sentence one\nSentence two]]> </string> </strings>
Does the above code looks flawed to you? Take a moment and look it through.
When you do the below AS2 codes in flash, what the \n does in the string is basically creating a newline, and thus "Sentence two" will appear below "Sentence one".
var txt:TextField = createTextField( "txt", _root.getNextHighestDepth(), 100, 100, 200, 200 ); txt.text = "Sentence one\nSentence two";
Happily, I ported all these original strings into an XML and got it loaded. What puzzled me was that, when these text were actually loaded, the \n didn’t work! The textfield simply displayed "Sentence one\nSentence two" without the newline. I sought help from a fellow colleague, Arul, and together, we still could not see what’s wrong with it. We left office without solving that "bug".
That night, Arul called to say, "Hey, with CDATA, we don’t need the \n! We simply just need to hit <enter>!" Immediately it struck me, oh ya! That’s the whole idea of using CDATA isn’t it? We got too engrossed with the wrong problem!
The next day, I changed all the \n to <enter> key, and expected things to work. Unfortunately though, the text did not turn out as expected. Flash had interpreted my <enter> in XML as double <enter>s when it’s being displayed on the textfield!
I sought Arul’s advice again and this is what I got. Flash interprets <enter> in external text sources as "\r\n", which explains the double <enter>s being rendered. So, what I actually had to do is to run through the string, strips off the "\r", and change it to "" (empty string).
Here’s the code that solves the above issue.
function processString ( s:String ):String { return s.split("\r").join(""); }
Hope this helps!
Comments(4)






