Writing Scripts with ActionScript > About variables
About variablesA 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 movie plays, you can record and save information about what the user has done, record values that change as the movie plays, or evaluate whether some 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 movie. Initializing variables makes it easier to track and compare the variable's value as the movie plays.
Variables can hold any type of data: number, string, Boolean, object, or movie clip. 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 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 movie and movie clip instance has its own set of variables, with each variable having its own value independent of variables in other movies or movie clips.
A variable's name must follow these rules:
![]() |
It must be an identifier. |
![]() |
It cannot be a keyword or a Boolean literal (true or false ). |
![]() |
It must be unique within its scope. (See Scoping a variable.) |
In Flash, you do not have to explicitly define a variable as holding either a number, a string, or other data type. Flash determines the data type of a variable when the variable is assigned:
x = 3;
In the expression x = 3
,
Flash
evaluates the element on the right side of the operator and determines that it is of type number. A later assignment may change the type of x
; for example, x = "hello"
changes the type of x
to a string. A variable that hasn't been assigned a value has a type of undefined
.
ActionScript converts data types automatically when an expression requires it. For example, when you pass a value to the trace
action, trace
automatically converts the value to a string and sends it to the Output window. In expressions with operators, ActionScript converts data types as needed; for example, when used with a string, the + operator expects the other operand to be a string:
"Next in line, number " + 7
ActionScript converts the number 7 to the string "7"
and adds it to the end of the first string, resulting in the following string:
"Next in line, number 7"
When you debug scripts, it's often useful to determine the data type of an expression or variable to understand why it is behaving a certain way. You can do this with the typeof
operator, as in this example:
trace(typeof(variableName
));
To convert a string to a numerical value, use the Number
function. To convert a numerical value to a string, use the String
function. See their individual entries in ActionScript dictionary: Overview.
A variable's "scope" refers to the area in which the variable is known and can be referenced. Variables in ActionScript can be either global or local. A global variable is shared among all Timelines; a local variable is only available within its own block of code (between the curly braces).
You can use the var
statement to declare a local variable inside a script. For example, the variables i
and j
are often used as loop counters. In the following example, i
is used as a local variable; it only exists inside the function makeDays
:
function makeDays(){ var i for( i = 0; i < monthArray[month]; i++ ) { _root.Days.attachMovie( "DayDisplay", i, i + 2000 ); _root.Days[i].num = i + 1; _root.Days[i]._x = column * _root.Days[i]._width; _root.Days[i]._y = row * _root.Days[i]._height; column = column + 1; if (column == 7 ) { column = 0; row = row + 1; } } }
Local variables can also help prevent name collisions, which can cause errors in your movie. For example, if you use name
as a local variable, you could use it to store a user name in one context and a movie clip instance name in another; because these variables would run in separate scopes, there would be no collision.
It's good practice to use local variables in the body of a function so that the function can act as an independent piece of code. A local variable is only changeable within its own block of code. If an expression in a function uses a global variable, something outside the function could change its value, which would change the function.
To declare global variables, use the setVariables
action or the assignment (=) operator. Both methods achieve the same results.
To declare local variables, use the var
statement inside the body of a function. Local variables are scoped to the block, and expire at the end of the block. Local variables not declared within a block expire at the end of their script.
Note: The call
action also creates a new local variable scope for the script it calls. When the called script exits, this local variable scope disappears. However, this is not recommended because the call
action has been replaced by the with
action which is more compatible with dot syntax.
To test the value of a variable, use the trace
action to send the value to the Output window. For example, trace(hoursWorked)
sends the value of the variable hoursWorked
to the Output window in test-movie mode. You can also check and set the variable values in the Debugger in test-movie mode. For more information, see Troubleshooting ActionScript: Overview.
You must declare a variable in a script before you can use it in an expression. If you use an undeclared variable, as in the following example, the variable's value will be undefined
and your script will generate an error:
getURL(myWebSite); myWebSite = "http://www.shrimpmeat.net";
The statement declaring the variable myWebSite
must come first so that the variable in the getURL
action can be replaced with a value.
You can change the value of a variable many times in a script. The type of data that the variable contains affects how and when the variable changes. Primitive data types, such as strings and numbers, are passed by value. This means that the actual content of the variable is passed to the variable.
In the following example, x
is set to 15 and that value is copied into y
. When x
is changed to 30, the value of y
remains 15 because y
doesn't look to x
for its value; it contains the value of x
that it was passed.
var x = 15; var y = x; var x = 30;
As another example, the variable in
contains a primitive value, 9, so the actual value is passed to the sqrt
function and the returned value is 3:
function sqrt(x){
return x * x;
}
var in = 9;
var out = sqr(in);
The value of the variable in
does not change.
The object data type can contain such a large and complex amount of information that a variable with this type doesn't hold the actual value; it holds a reference to the value. This reference is like an alias that points to the contents of the variable. When the variable needs to know its value, the reference asks for the contents and returns the answer without transferring the value to the variable.
The following is an example of passing by reference:
var myArray = ["tom", "dick"]; var newArray = myArray; myArray[1] = "jack"; trace(newArray);
The above code creates an Array object called myArray
that has two elements. The variable newArray
is created and passed a reference to myArray
. When the second element of myArray
is changed, it affects every variable with a reference to it. The trace
action would send ["tom", "jack"]
to the Output window.
In the next example, myArray
contains an Array object, so it is passed to function zeroArray
by reference. The zeroArray
function changes the content of the array in myArray
.
function zeroArray (array){ var i; for (i=0; i < array.length; i++) { array[i] = 0; } } var myArray = new Array(); myArray[0] = 1; myArray[1] = 2; myArray[2] = 3; var out = zeroArray(myArray)
The function zeroArray
accepts an Array object as an argument and sets all the elements of that array to 0. It can modify the array because the array is passed by reference.
References to all objects other than movie clips are called hard references because if an object is referenced, it cannot be deleted. A reference to a movie clip is a special kind of reference called a soft reference. Soft references do not force the referenced object to exist. If a movie clip is destroyed with an action such as removeMovieClip
, any reference to it will no longer work.