Method bodies, frame scripts and handlers are lists of statements. ActionScript 2.0 has every statement of ActionScript 1.0 in Flash MX and adds exception handling. The declarations that make up a class file are described with Classes, Interfaces and Packages.
Grammar
expr is any expression, stmt one statement or a block in braces, type a class or interface name, and square brackets mark optional parts.
statement = block | var-stmt | expr ";" | if-stmt | loop | for-in | switch-stmt
| "break" ";" | "continue" ";" | "return" [expr] ";"
| function-decl | with-stmt | try-stmt | "throw" expr ";" | ";"
block = "{" { statement } "}"
var-stmt = "var" name [":" type] ["=" expr] { "," name [":" type] ["=" 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 [":" type] "in" expr ")" stmt
switch-stmt = "switch" "(" expr ")" "{" { "case" expr ":" { statement } }
[ "default" ":" { statement } ] "}"
function-decl = "function" name "(" [param { "," param }] ")" [":" type] block
param = name [":" type]
with-stmt = "with" "(" expr ")" stmt
try-stmt = "try" block { "catch" "(" name [":" type] ")" block } [ "finally" block ]
try needs at least one
catch or a finally.Every statement
| Statement | Does | Player |
|---|---|---|
var | Declares variables, optionally typed and given starting values. | 5 |
if … else | Runs one statement or the other by a condition. | 4 |
while, do … while | Repeat while a condition is true, testing before or after each pass. | 4 |
for | The counting loop. | 5 |
for … in | Runs once for each enumerable property name of an object. The methods of ActionScript 2.0 classes are hidden from it. | 5 |
break, continue | Leave the innermost loop or switch, or start the loop’s next pass. There are no labels. | 4 |
return | Leaves a function with a value, which must suit its declared return type. | 5 |
switch, case, default | Jumps to the matching case, or to default, and runs on until break. | 4, 6 |
with | Puts an object at the front of the scope chain for its body. | 5 |
function | Declares a named function; see Functions. | 5 |
try, catch, finally | Runs a block, catches values thrown in it, and runs the finally block whatever happens. | 7 |
throw | Throws a value, usually an Error or a subclass of it. | 7 |
tellTarget, ifFrameLoaded | Deprecated Flash 4 statements, still accepted in FLA scripts. | 3, 4 |
Exceptions
Any value can be thrown, but the convention is an Error object, which
has name and message properties, or an instance of a
subclass of Error. A try block can have several
catch blocks with typed parameters; the first whose type fits the thrown
value runs, and an untyped one catches everything. An exception that nothing catches
stops the rest of the script it was thrown in.
class com.example.data.ParseError extends Error {
public var line:Number;
public function ParseError(message:String, line:Number) {
super(message);
this.name = "ParseError";
this.line = line;
}
}
// elsewhere
try {
settings = parser.read(text);
} catch (e:ParseError) {
trace("Bad settings on line " + e.line);
} catch (e) {
trace("Unexpected: " + e);
} finally {
loading_mc._visible = false;
}
try with two
catch blocks.Flash Player does not throw exceptions for mistakes in ActionScript 2.0 code, such as
calling a method that does not exist or reading a property of
undefined; those still fail quietly. Only throw statements,
in user code or in classes such as the version 2 components, throw. Exceptions need a
SWF file for Flash Player 7 or later.
Handlers and directives
In FLA scripts only, as in ActionScript 1.0:
| Construct | Where |
|---|---|
on (event) { … } | The script of a button or movie clip instance; see Events |
onClipEvent (event) { … } | The script of a movie clip instance |
#include "file.as" | Any FLA script or include file, but not a class file |
#initclip … #endinitclip | The first frame of a symbol; with ActionScript 2.0 rarely written by hand, since the compiler makes these blocks for classes |