Chapter 18. On-Screen Text Fields
Because Flash is fundamentally a visual environment,
movies often present on-screen information to users. Similarly,
because Flash is an interactive environment, movies often retrieve
information from users through a GUI. To display the value of a
variable on screen or to allow a user to type data into a Flash
movie, we use text fields.
Text fields provide a means of both setting and retrieving the values
of variables that have a visual representation. Text fields come in
two varieties -- dynamic text fields, which we use to display
information to the user, and user-input text fields, which we use to
retrieve information from the user.
18.1. Dynamic Text Fields
A dynamic text field is like a variable viewport -- it displays
the value of a specified variable as a text string. Dynamic text
fields are created using the
Text tool in Flash.
However, unlike regular static text, the content of a dynamic text
field is connected to a variable and can be changed or retrieved via
ActionScript.
By retrieving a text field's value, we can capture on-screen
information for use in a script. By setting a text field's
value, we cause that value to display on screen.
18.1.1. Creating a Dynamic Text Field
To make a new
dynamic text field, follow these steps:
Select the Text tool. Click and drag a rectangle on the Stage. The outline that you create
will define the size of the new text field. Select Text Options. The Text Options panel appears. In the Text Type menu, choose Dynamic Text (the other options are
Static Text and Input Text). Under Variable, type a name for the dynamic text field, following the
rules we learned in Chapter 2, "Variables", for constructing
legal variable names.
After creating a dynamic text field, you'd normally set the new
field's options, as described later.
18.1.2. Changing the Content of a Dynamic Text Field
Once a text field is created, we can use it to
display a value on the screen. For example, if we create a dynamic
text field named myText, we can set the content of
that text field using the following statements:
myText = 10; // Display a number in the text field myText
myText = "Welcome to my web site"; // Display a string instead
var msg = "Please make a selection";
myText = msg; // Display the value of msg in myText
Whenever the value of the variable myText changes,
the content of the myText dynamic text field
updates to reflect the change. However, before a value is sent to a
dynamic text field for display, it is first converted to a string.
The actual content is therefore governed by string-conversion rules
described in Table 3-2.
Like normal variables, text fields are tied to the movie clip
timeline on which they reside. To access a dynamic text field in a
remote movie clip timeline, we use the techniques described in Chapter 2, "Variables" under Section 2.5.6, "Accessing Variables on Different Timelines".
18.1.3. Retrieving the Value of a Dynamic Text Field
We can retrieve a dynamic text field's
value by simply using its name. For example, if
myTextField were a dynamic text field in our
movie, we could retrieve and assign its value to another variable
like so:
welcomeMessage = myTextField;
Text field assignment and retrieval are often combined in one
statement. You can use the += operator to append
text to a text field's current contents:
// Set a text field's value
myTextField = "Today's Headlines...";
// Create a new message
var newText = "Update! The Party Has Been Cancelled!"
// Add the new message to the existing text field content
myTextField += newText;
 |  |  | | 17.3. Onward! |  | 18.2. User-Input Text Fields |
Copyright © 2002 O'Reilly & Associates. All rights reserved.
|