User Guide
44 Chapter 2: ActionScript Basics
About variables
A variable is a container that holds information. The container itself is always the same, but the
contents can change. By changing the value of a variable as the SWF file plays, you can record and
save information about what the user has done, record values that change as the SWF file plays, or
evaluate whether a condition is true or false.
It’s a good idea always to assign a variable a known value the first time you define the variable.
This is known as initializing a variable and is often done in the first frame of the SWF file. If a
variable is declared in the FLA (instead of in an external file), it works only for the frame where
the variable is declared. Only the frame code for that frame is scanned, so the second frame would
not recognize the variable if it was referenced. Initializing a variable helps you track and compare
the variable’s value as the SWF file plays. Flash Player 7 and later evaluate uninitialized variables
differently than Flash Player 6 and earlier. If you have written scripts for Flash Player 6 and plan
to write or port scripts for Flash Player 7 or later, you should be understand these differences to
avoid unexpected behavior. For more information, see “ECMA-262 compliance” on page 14.
Variables can hold any type of data (see “About data types” on page 34). The type of data a
variable contains affects how the variable’s value changes when it is assigned in a script.
Typical types of information that you can store in a variable include a URL, a user’s name, the
result of a mathematical operation, the number of times an event occurred, or whether a button
has been clicked. Each SWF file and movie clip instance has a set of variables, with each variable
having a value independent of variables in other SWF files or movie clips.
To view the value of a variable, use the
trace() statement to send the value to the Output panel.
For example,
trace(hoursWorked) sends the value of the variable hoursWorked to the Output
panel in test mode. You can also check and set the variable values in the Debugger in test mode.
For more information, see “Using the trace statement” on page 165 and “Displaying and
modifying variables” on page 157.
For more information, see the following topics:
• “Naming a variable” on page 44
• “Scoping and declaring variables” on page 45
• “Using variables in a program” on page 47
Naming a variable
A variable’s name must follow these rules:
• It must be an identifier (see “Terminology” on page 24).
• It cannot be a keyword or an ActionScript literal such as true, false, null, or undefined.
(See Chapter 3, “Avoiding reserved words,” on page 75).
• It must be unique within its scope (see “Scoping and declaring variables” on page 45).