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 runtime” on 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 couldn’t 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 you’re
targeting a version of Flash Player that doesn’t support the TextField class. This will lessen the
chances of a variable name conflict, which could result in unexpected behavior at runtime.
Summary of Contents for FLASH MX 2004 - ACTIONSCRIPT
Page 1: ...ActionScript Reference Guide...
Page 8: ...8 Contents...
Page 12: ......
Page 24: ...24 Chapter 1 What s New in Flash MX 2004 ActionScript...
Page 54: ...54 Chapter 2 ActionScript Basics...
Page 80: ...80 Chapter 3 Writing and Debugging Scripts...
Page 82: ......
Page 110: ...110 Chapter 5 Creating Interaction with ActionScript...
Page 112: ......
Page 120: ...120 Chapter 6 Using the Built In Classes...
Page 176: ......
Page 192: ...192 Chapter 10 Working with External Data...
Page 202: ...202 Chapter 11 Working with External Media...
Page 204: ......
Page 782: ...782 Chapter 12 ActionScript Dictionary...
Page 793: ...Other keys 793 221 222 Key Key code...
Page 794: ...794 Appendix C Keyboard Keys and Key Code Values...
Page 798: ...798 Appendix D Writing Scripts for Earlier Versions of Flash Player...
Page 806: ...806 Appendix E Object Oriented Programming with ActionScript 1...
Page 816: ...816 Index...