One of the nice things about text fields is that now because they are actual objects in ActionScript, you can create and destroy them dynamically. And to create them in AS, we use the createTextField() method, which works like this:
Movie.createTextField(instanceName, depth, x, y, width, height);
Because text fields must be placed within movie clips, Movie represents the movie clip that the text field will reside in. The first parameter we set is the instance name; remember to use the suffix "_txt" to activate code hints. The next parameter is the depth, or stacking order, of the text field. The next two parameters are its coordinates, and they represent the top left corner of the text field. The final two parameters are the width and height of the text field.
Let's jump right in and create one in ActionScript.
Now test the movie once more, and you will see the text in the text field at the top left corner of the screen. And because the createTextField() method returns a reference to the text field, we could have also written the code like this:
//this will create a text field on the root layer var theText:TextField = this.createTextField("myText_txt",1,0,0,120,20); //set the text using the reference theText.text = "I created this with code";
Both ways will work the same.
Looking at the text in the test movie screen, notice that the letters are black and appear to be in Times New Roman. That is because when you create text fields in ActionScript they have a few default values:
Now you know how to change some of the features, so let's go back into our example and give it a border and a background, as well as change the text color.
When you test the movie again, you will see a border around the text field. And the text field formatting could have been applied before setting the text property. But in order to be able to change the color of the border and the background with ActionScript, both properties must be set to true or the border will not show.
Now that you've seen how to create text fields manually as well as with ActionScript, and you know how to change properties of the text field itself, let's look at how to change the formatting of the text in the text field.