User Guide

222 Chapter 9: Working with Text
To control a dynamic or input text field using ActionScript, you must assign it an instance name
in the Property inspector. You can then reference the text field with the instance name, and use
the methods and properties of the TextField class to control the contents or basic appearance of
the text field.
You can also create TextField objects at runtime, and assign them instance names, using the
MovieClip.createTextField() method. For more information, see “Creating text fields at
runtime” on page 223.
For more information on using the TextField class, see the following topics:
Assigning text to a text field at runtime” on page 222
About text field instance and variable names” on page 222
Assigning text to a text field at runtime
To assign text to a text field, use the
TextField.text property.
To assign text to a text field at runtime:
1.
Using the Text tool, create a text field on the Stage.
2.
With the text field selected, in the Property inspector (Window > Properties), select Input Text
from the Text Type pop-up menu, and enter
headline_txt in the Instance Name text box.
Instance names can consist only of letters, numbers, underscores (_), and dollar signs ($).
3.
In the Timeline, select the first frame in Layer 1, and open the Actions panel
(Window > Development Panels > Actions).
4.
Type the following code in the Actions panel:
headline_txt.text = "Brazil wins World Cup";
5.
Select Control > Test Movie to test the movie.
About text field instance and variable names
In the Instance Name text box in the Property inspector, you must assign an instance name to a
text field to invoke methods and get and set properties on that text field.
In the Var text box in the Property inspector, you can assign a variable name to a dynamic or
input text field. You can then assign values to the variable.
Do not confuse a text field’s instance name with its variable name, however. A text field’s variable
name is simply a variable reference to the text contained by that text field; it is not a reference to
an object.
For example, if you assigned a text field the variable name
mytextVar, you can set the contents of
the text field using the following code:
var mytextVar:String = "This is what will appear in the text field";