About variables

A variable is a container that holds information. The following ActionScript shows what a variable looks like in ActionScript:

var myVariable:Number = 10;

This variable holds a numerical value. The use of :Number in the previous code assigns the type of value that variable holds, called data typing. For more information on data typing, see About assigning data types and strict data typing and Assigning a data type.

The container (represented by the variable name) is always the same throughout your ActionScript, but the contents (the value) can change. You can change the value of a variable in a script as many times as you want. When you change the value of a variable while 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. You might need the variable to continually update while the SWF file plays, such as when a player's score changes in a Flash game. Variables are essential when you create and handle user interaction in a SWF file.

It's a good idea to assign a value to a variable the first time you declare the variable. Assigning an initial value is called initializing the variable, and it's often done on Frame 1 of the Timeline or from within a class that loads when the SWF file begins to play. There are different kinds of variables, which are affected by scope. For more information on different kinds of variables and scope, see About variables and scope.

TIP

 

Initializing a variable helps you track and compare the variable's value as the SWF file plays.

NOTE

 

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.

Variables can hold different types of data; for more information, see About data types. The type of data that a variable contains affects how the variable's value changes when you assign that value in a script.

Typical types of information that you can store in a variable include a URL (String type), a user's name (String type), the result of a mathematical operation (Number type), the number of times an event occurred (Number type), or whether a user has clicked a particular button (Boolean type). Each SWF file and object instance (such as a movie clip) 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. Then, the value displays in the Output panel when you test the SWF file in the test environment. For example, trace(hoursWorked) sends the value of the variable hoursWorked to the Output panel in the test environment. You can also check and set the variable values in the Debugger in the test environment.

For more information on variables, see the following topics:


Version 8

 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/8/main/00001192.html