In ActionScript 1.0 most variables belong to a timeline. Each movie clip, and each
main movie, is an object, and the variables set by its frame scripts are properties of
it, next to its built-in properties such as _x. Functions add local
variables, and Flash MX adds a global object.
Declaring variables
| Written | In a frame script | In a function |
|---|---|---|
score = 0; | A variable of this timeline | Sets a local variable of that name if there is one; otherwise a variable of the timeline the function was defined on |
var score = 0; | Also a variable of this timeline | A local variable, gone when the function returns |
_global.score = 0; | A global variable, seen from every timeline (Flash MX) | |
_root.score = 0; | A variable of the main timeline, from anywhere | |
A variable that has never been set reads as undefined, without an error.
So a misspelt name fails quietly, and Flash 5’s case-insensitive names make many
spellings the same variable; see
Case sensitivity.
A timeline variable lasts as long as its movie clip. When the clip is removed from the stage, or the playhead leaves the frames where it exists, its variables go with it. Variables of the main timeline last until the movie is unloaded.
How a name is found
An unqualified name is looked up along a scope chain, and the first object that has it wins:
- in a function, the function’s local variables and parameters;
- the objects added by any enclosing
withstatements, innermost first; - the timeline that holds the code: for a frame script, its own clip; for a function,
the clip it was defined on; for an
on()handler on a button, the timeline the button is on; for anonClipEvent()handler, the clip itself; - the global object, which holds the built-in classes and functions, and from Flash MX
everything set on
_global.
The parent timeline is not in the chain: a clip’s script does not see its parent’s
variables without _parent. A timeline variable hides a global one of the
same name. Assigning to an unqualified name never creates a global; it creates a
timeline variable, so globals are set as _global.name.
A function’s timeline is fixed where it is defined, not where it is called. A
function defined on _root and called as _root.reset() from a
button inside a clip still works on _root’s variables. In Flash 5,
assigning a function to a variable on another timeline moves its scope to that
timeline.
Built-in references
| Name | Means | Player |
|---|---|---|
this | The current object: the clip in a frame script or onClipEvent(), the timeline holding the button in on(), the object a method was called on, the new object in a constructor. | 5 |
_root | The main timeline of the level the code is in. | 5 |
_parent | The clip that contains this one. _parent._parent goes up two levels. | 5 |
_level0, _level1 … | The main timeline of a movie loaded into that level with loadMovieNum. The first movie is _level0. | 4 |
_global | The global object. | 6 |
arguments | Inside a function, the arguments of the current call. | 5 |
Paths: dot syntax and slash syntax
A clip is reached by the chain of instance names from a timeline down to it. Flash 5
brought dot syntax; Flash 4 used slash syntax, which ActionScript 1.0 still accepts in
strings given to tellTarget, eval, set and the
loading functions, and in variable names written with a colon.
| Dot syntax | Slash syntax | Means |
|---|---|---|
_root | / | The main timeline |
_root.menu.item1 | /menu/item1 | A clip by its absolute path |
menu.item1 | menu/item1 | A clip by a path relative to this timeline |
_parent | .. | The parent timeline |
_parent.logo | ../logo | A sibling clip |
_root.score | /:score | A variable of the main timeline |
_root.menu.count | /menu:count | A variable of a clip: the colon separates the path from the variable |
_level1.intro | _level1/intro | A clip in another level |
targetPath(clip) gives a clip’s path in dot syntax, such as
"_level0.menu.item1"; a clip’s _target property gives it in
slash syntax, "/menu/item1".
// Flash 4 style, still accepted
tellTarget ("/menu") {
gotoAndStop(2);
}
/:score = /:score + 10;
// The same in Flash 5 style
_root.menu.gotoAndStop(2);
_root.score += 10;
tellTarget, and its dot syntax
equivalent.Dynamic names: eval, set and brackets
Names built while the movie runs are read and written in three ways:
| Form | Does |
|---|---|
eval("item" + i) | Returns the variable, property or clip with that name or path, or undefined. The string may be a path in either syntax. ActionScript’s eval only looks names up; unlike JavaScript’s, it does not run code, and in Flash 5 and later it cannot be the target of an assignment. |
set("item" + i, value) | Sets the variable with that name or path; the form of Flash 4’s Set Variable action. |
this["item" + i] | Reads or sets a property of an object by name, the usual form from Flash 5 on. |
for (var i = 1; i <= 5; i++) {
set("score" + i, 0); // score1 … score5 on this timeline
this["star" + i]._alpha = 50; // clips star1 … star5
}
trace(eval("score" + 3)); // 0
Variables from outside the movie
loadVariables(url, target)andloadVariablesNum(url, level)load URL-encoded text, such asname=Ann&score=12, and set each pair as a variable of the target timeline. Every value arrives as a string.- From Flash MX,
LoadVarsobjects load and send the same text into an object instead of a timeline; see Built-in classes. - From Flash Player 6, the
FlashVarsparameter of the HTML page that embeds the movie sets variables on the main timeline before the first frame.