About text fields
387
Do not confuse a text field’s instance name with its variable name, however. A text field’s
variable name is 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 use the
following code to set the contents of the text field:
var myTextVar:String = "This is what will appear in the text field";
However, you can’t use the variable name
myTextVar
to set the text field’s
text
property. You
have to use the instance name, as shown in the following code:
// This won't work.
myTextVar.text = "A text field variable is not an object reference";
// For input text field with instance name "myField", this will work.
myField.text = "This sets the text property of the myField object";
Use the
TextField.text
property to control the contents of a text field, unless you’re
targeting a version of Flash Player that doesn’t support the TextField class. This reduces the
chances of a variable name conflict, which could result in unexpected behavior at runtime.
You can find sample source files that demonstrate how to work with text fields using
ActionScript. The source files are called textfieldsA.fla and textfieldsB.fla, and you can find
them in the Samples folder on your hard disk:
■
In Windows, browse to
boot drive
\Program Files\Macromedia\Flash 8\Samples and
Tutorials\Samples\ActionScript\TextFields.
■
On the Macintosh, browse to
Macintosh HD
/Applications/Macromedia Flash 8/Samples
and Tutorials/Samples/ActionScript/TextFields.
Creating text fields at runtime
You can use the
createTextField()
method of the MovieClip class to create an empty text
field on the Stage at runtime. The new text field is attached to the timeline of the movie clip
that calls the method.
To dynamically create a text field using ActionScript:
1.
Select File > New and then select Flash Document to create a new FLA file.
2.
Type the following ActionScript on Frame 1 of the Timeline:
this.createTextField("test_txt", 10, 0, 0, 300, 100);
This code creates a 300 x 100-pixel text field named
test_txt
with a location of (0, 0)
and a depth (
z
-order) of 10.
Summary of Contents for FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Page 1: ...Learning ActionScript 2 0 in Flash...
Page 8: ...8 Contents...
Page 18: ...18 Introduction...
Page 30: ...30 What s New in Flash 8 ActionScript...
Page 66: ...66 Writing and Editing ActionScript 2 0...
Page 328: ...328 Interfaces...
Page 350: ...350 Handling Events...
Page 590: ...590 Creating Interaction with ActionScript...
Page 710: ...710 Understanding Security...
Page 730: ...730 Debugging Applications...
Page 780: ...780 Deprecated Flash 4 operators...
Page 830: ...830 Index...