ActionScript 2.0 has two kinds of code with different scopes. Code in class files works with local variables and class members, resolved by the compiler. Code in the FLA, frame scripts and handlers, works with timeline variables as in ActionScript 1.0. Both share the global object.
Kinds of variable
| Kind | Declared | Lives |
|---|---|---|
| Local | var n:Number = 0; inside a function or method | Until the function returns, or longer if a nested function uses it |
| Instance member | var name:String; in a class body, outside methods | As long as the object; see Classes |
| Static member | static var count:Number = 0; in a class body | As long as the movie, on the class itself |
| Timeline | var score:Number = 0; or score = 0; in a frame script | As long as the movie clip |
| Global | _global.settings = {…}; | As long as the movie |
A variable declared but never set is undefined, whatever its type.
Names in class methods
Inside a method, the compiler resolves an unqualified name in this order:
- the method’s parameters and local variables;
- the instance members of the class and of its superclasses, which it compiles as
this.name, and the static members, compiled asClassName.name; - imported and fully named classes;
- anything else is left to be looked up when the code runs, on the global object.
So members need no this. in front, though writing it is allowed. Timeline
variables are not in scope in a class; a class reaches them by a path, such as
_root.score or, in a movie clip subclass, _parent.score.
Assigning to an unknown unqualified name in a method does not make it a member of the
object, so a misspelt member name can slip through.
Names in FLA scripts
Frame scripts and handlers follow the rules of ActionScript 1.0: locals, any
with objects, the timeline the code belongs to, then the global object.
Code on a frame sees its own timeline’s variables, not its parent’s. A function defined
on a timeline keeps that timeline in its scope wherever it is called from. Variables
there may be typed, but only uses the compiler can follow are checked.
Built-in references
| Name | Means | Player |
|---|---|---|
this | In a method, the object it was called on; in a constructor, the new object; in a frame script, the timeline | 5 |
super | The superclass, for calling its constructor and methods | 6 |
_root | The main timeline of the current level, or of a clip with _lockroot set | 5 |
_parent | The containing clip | 5 |
_level0, _level1 … | The main timeline of each loaded level | 4 |
_global | The global object, which also holds every class by its package path | 6 |
clip._lockroot | When true, _root inside a loaded movie means that movie, not the one that loaded it | 7 |
Dynamic names
Square brackets read and set a property by a name built at run time:
this["item" + i]. The compiler does not check bracket access, so it also
works on objects of classes that are not dynamic. eval(name)
and set(name, value) still work in FLA scripts, and paths in slash syntax
are still accepted in strings given to eval and the loading functions, but
both are relics of Flash 4.
Variables from outside
- The
FlashVarsparameter of the HTML page, and a query string on the SWF’s URL, set variables on_rootbefore frame 1. LoadVarsloads URL-encoded variables into an object; every value arrives as a string and needs converting before use as a typed number.SharedObject.getLocal(name).datakeeps values on the user’s computer between visits.