Flash 9 TextFields and the added a carriage return character
It seems that the Textfields in Flash 9/CS3 give you a little something extra: the carriage return (CR) character. It is automatically added to the end of the text value of each Textfield. Even if the Textfield is empty it will still have a CR.
This shouldn’t be a problem for most cases and only applies if you create dynamic Textfields at authoring time. Textfields created at runtime are truly empty when you create them.
The main case where you might have an issue with this is if you need to test a Textfield created at author time to see if it’s empty. If you write the following code you will get the else result.
Now if you want to test and see if the Textfield is empty you will have to do this:
Another problem you might have is if you want to append text ( someTF.text += someString ) to a Textfield created at authoring time from various locations in your code. If you don’t know when you will append first your first line of text will appear one line lower than you expected. You could solve the problem with:
You could also solve this problem with an init() function whereby you make all your authoring time Textfields equal to an empty string. If there are only a few Textfields it’s nothing to just target them individually. Otherwise you might want to do a for(in loop) like such:
There is only one problem: this will wipe out the text of any static Textfields you might have as well. You can’t give a static TextField an instance name at authoring time but they do get instance names at runtime along with all the other Stage objects: instance1, instance2, instance3, …
The only way that you could wipe out only the dynamic Textfields would be to make sure that they all have authoring time instance names and then test to make sure that the instance name doesn’t begin with the string “instance”. Here is an example:
Now you might be asking, what do I do if I have created dynamic Textfields all over the place at authoring time and I want to blank them all out in one fell swoop. Is that possible?
Well yes. Try this:
That code snippet will recursively run through every MovieClip placed on the Stage at authoring time. So this might be a kick in the pants to your startup time if you have a huge SWF with hundreds, or even thousands, of embedded MovieClips.
I’ll leave it as an exercise for the reader to make their own class out of this if they wish.
If something here has proved valuable to you then feel free to drop a couple of bucks in the tip-jar.






