View comments | RSS feed

Scoping and declaring variables

A variable's scope refers to the area in which the variable is known and can be referenced. There are three types of variable scopes in ActionScript:

For guidelines on using scope and variables, see Using scope and Avoiding _root.

Note: ActionScript 2.0 classes that you create support public, private, and static variable scopes. For more information, see Controlling member access and Creating class members.

Timeline variables

Timeline variables are available to any script on that Timeline. To declare Timeline variables, use the var statement and initialize them in any frame in the Timeline; the variable will be available to that frame and all following frames, as shown in the following example:

var x:Number = 15; //initialized in Frame 1, so it's available to all frames

Make sure to declare a Timeline variable before trying to access it in a script. For example, if you put the code var x = 10; in Frame 20, a script attached to any frame before Frame 20 cannot access that variable.

Local variables

To declare local variables, use the var statement inside the body of a function. A local variable declared within a function block is defined within the scope of the function block and expires at the end of the function block.

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 exists only inside the function initArray():

var myArray:Array = [ ];
function initArray(arrayLength:Number) {
   var i:Number;
   for( i = 0; i < arrayLength; i++ ) {
      myArray[i] = i + 1;
      
   }
}

Local variables can also help prevent name conflicts, which can cause errors in your application. For example, if you use age as a local variable, you could use it to store a person's age in one context and the age of a person's child in another; because these variables would run in separate scopes, there would be no conflict.

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 changeable only within its own block of code. If an expression in a function uses a global variable, something outside the function can change its value, which would change the function.

You can assign a data type to a local variable when you define it, which helps prevent you from assigning the wrong type of data to an existing variable. For more information, see Strict data typing.

Global variables

Global variables and functions are visible to every Timeline and scope in your document. To create a variable with global scope, use the _global identifier before the variable name and do not use the var = syntax. For example, the following code creates the global variable myName:

var _global.myName = "George"; // incorrect syntax for global variable
_global.myName = "George"; // correct syntax for global variable

However, if you initialize a local variable with the same name as a global variable, you don't have access to the global variable while you are in the scope of the local variable, as shown in the following example:

_global.counter = 100; // declares global variable
trace(counter); // accesses the global variable and displays 100
function count(){
   for( var counter = 0; counter <= 2 ; counter++ ) { //local variable
   trace(counter); // accesses local variable and displays 0 through 2
   }
}
count();
trace(counter); // accesses global variable and displays 100

This example simply shows that the global variable is not accessed in the scope of the count() function. To avoid confusion in your applications, name your variables uniquely.

The Flash Player version 7 and later security sandbox enforces restrictions when accessing global variables from movies loaded from separate security domains. For more information, see Flash Player security features.


Comments


CaptainFreedom said on Jun 7, 2005 at 6:43 AM :
One of the biggest handycaps of Actionscript is that there is no way to force the compilor to check that variable is declared before being used. For example, if you spell a variable wrong you can waste a lot of time looking for the error if the compilor doesn't flag it.
No screen name said on Jun 14, 2005 at 1:32 PM :
I try use a _global variable on movie loaded by loadmovie, and it's not recognized the variabel... what can i do?
matsoup said on Feb 17, 2006 at 3:20 AM :
"if you initialize a local variable with the same name as a global variable, you don't have access to the global variable while you are in the scope of the local variable, as shown in the following example:"

This is partly true - you can still access the global variable (even though it has the same name) by prefixing the variable name with _global:

//----------------------------------------------------

_global.hello = "i'm a global variable";
trace(hello);
test();

function test(){
var hello:String = "i'm a local variable";
trace(hello);
trace("accessing directly using _global");
trace(_global.hello);
}

 

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

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00000785.html