A script is a list of statements, run in order each time its frame is reached or its
handler fires. ActionScript 1.0 has ECMA-262’s statements except exception handling and
labels, with switch arriving in Flash MX, and keeps two statements from
Flash 4.
Grammar
In this summary, expr is any expression, stmt one statement or a block of them in braces, and square brackets mark optional parts.
statement = block | var-stmt | expr ";" | if-stmt | loop | for-in
| "break" ";" | "continue" ";" | "return" [expr] ";"
| function-decl | with-stmt | switch-stmt
| tell-target | if-frame-loaded | ";"
block = "{" { statement } "}"
var-stmt = "var" name ["=" expr] { "," name ["=" expr] } ";"
if-stmt = "if" "(" expr ")" stmt [ "else" stmt ]
loop = "while" "(" expr ")" stmt
| "do" stmt "while" "(" expr ")" ";"
| "for" "(" [init] ";" [expr] ";" [expr] ")" stmt
for-in = "for" "(" ["var"] name "in" expr ")" stmt
function-decl = "function" name "(" [name { "," name }] ")" block
with-stmt = "with" "(" expr ")" stmt
switch-stmt = "switch" "(" expr ")" "{" { "case" expr ":" { statement } }
[ "default" ":" { statement } ] "}"
tell-target = "tellTarget" "(" expr ")" stmt
if-frame-loaded = "ifFrameLoaded" "(" [expr ","] expr ")" stmt
Every statement
| Statement | Does | Player |
|---|---|---|
var | Declares variables, with optional starting values. Local inside a function, a timeline variable elsewhere; see Variables. | 5 |
if … else | Runs one statement or the other by a condition. else if chains are an if inside the else. | 4 |
while | Repeats while a condition is true, testing it first. | 4 |
do … while | Repeats while a condition is true, testing it after each pass, so the body runs at least once. | 4 |
for | Runs the start expression, then repeats while the test is true, running the update after each pass. | 5 |
for … in | Runs once for each enumerable property of an object, setting the variable to the property’s name as a string. Inherited properties are included; built-in ones are hidden. Over a movie clip it lists its variables and named child clips. The order is not defined. | 5 |
break | Leaves the innermost loop or switch. | 4 |
continue | Starts the next pass of the innermost loop. | 4 |
return | Leaves a function, giving a value or undefined. | 5 |
function | Declares a named function; see Functions. | 5 |
with | Puts an object at the front of the scope chain for its body, so its properties and methods can be named without a path. Assigning to a name the object does not have sets a variable further down the chain, not a new property. | 5 |
switch, case, default | Jumps to the first case whose value matches the expression, or to default, and runs on until break. Added in Flash MX; it compiles to comparisons and jumps, and Macromedia’s reference lists switch and case as running in Flash Player 4, default in 6. | 4 |
tellTarget | Deprecated. Runs its body with another timeline as the target, given as a path string: tellTarget("/menu") { play(); }. Use dot syntax or with. | 3 |
ifFrameLoaded | Deprecated. Runs its body only if the given frame, optionally of a given scene, has downloaded. Use _framesloaded. | 4 |
expr ; | An assignment, call or other expression, run for its effect. | 4 |
; | The empty statement. | 4 |
Statements of ECMA-262 that ActionScript 1.0 does not have: try,
catch, finally and throw, which came with Flash MX
2004 and Flash Player 7, and labelled statements, so break and
continue always act on the innermost loop. Flash 5 also lacks
switch; a chain of if and else if does the same
work.
switch (Key.getCode()) {
case Key.LEFT:
ship._x -= 5;
break;
case Key.RIGHT:
ship._x += 5;
break;
default:
trace("Other key");
}
for (var name in settings) {
trace(name + " = " + settings[name]);
}
switch of Flash MX, and a for..in loop over an
object’s properties.Handlers
Two more constructs look like statements but can only be the outermost code of a button’s or movie clip instance’s script in the FLA:
| Handler | Attached to | Player |
|---|---|---|
on (event, …) { … } | A button instance, and from Flash MX a movie clip instance | 2 |
onClipEvent (event) { … } | A movie clip instance | 5 |
Their events are listed on Events.
Directives
Lines starting with # are read by the compiler before the script, take
no semicolon and are not statements:
| Directive | Does | Player |
|---|---|---|
#include "file.as" | Pastes in the text of a file; see The .as file. | 4 |
#initclip [order] | Starts a block of code that runs once when a symbol is first used. Flash MX. | 6 |
#endinitclip | Ends it. | 6 |