EasyManua.ls Logo

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT - Page 136

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT
816 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
136 Chapter 8: 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 runtimeon page 137.
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), enter
headline_txt in the Instance Name text box, directly below the Text Type pop-up menu on
the left side of the inspector.
Instance names can consist only of letters, 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 Property inspector, you can also assign a variable name to a dynamic or input text field, as
well as an instance name. You can then refer to the text field’s variable name in ActionScript,
whose value determines the text field’s contents. A text field’s instance name and variable name
should not be confused, however.
You use the instance name assigned to a text field to invoke methods and get and set properties on
that text field. 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 could then set the
contents of the text field using the following code:
var mytextVar = "This is what will appear in the text field";
However, you couldnt use the mytextVar variable to set the same text field’s text property to
some text.
//This won’t work
myTextVar.text = "A text field variable is not an object reference";
In general, use the TextField.text property to control the contents of a text field, unless youre
targeting a version of Flash Player that doesnt support the TextField class. This will lessen the
chances of a variable name conflict, which could result in unexpected behavior at runtime.

Table of Contents

Related product manuals