ActionScript 2.0 needs no new virtual machine. The compiler checks the classes and types, then writes the same kind of AVM1 bytecode as for ActionScript 1.0, building each class out of a constructor function and a prototype. The result runs in Flash Player 6 and later, best in 7 and later.
Compilers
| Compiler | Notes |
|---|---|
| Flash MX 2004, Flash 8, Flash CS3 to CS6 | Built into the authoring program; compiles when the movie is published or tested, or when syntax is checked. |
| MTASC | Motion-Twin’s free command-line compiler, written in OCaml, from 2004. Much faster than Flash’s and stricter about types. It compiles classes into an existing SWF file or a new one, and was used with FlashDevelop and build scripts. |
| Flex 1.0 and 1.5 | Macromedia’s server product compiled MXML and ActionScript 2.0 into SWF files for Flash Player 7 when a page was requested. |
Publish settings
| Setting | Effect |
|---|---|
| ActionScript version | 1.0 or 2.0. With 2.0, class files are found and compiled, annotations are checked and the compiler is case-sensitive. |
| Version | The Flash Player to publish for: 6 or later for ActionScript 2.0. It sets the SWF version, which bytecode actions may be used, and which conversion and case rules the player applies. |
| Settings: export frame for classes | The frame in which compiled classes are placed, 1 by default. |
| Settings: classpath | The document classpath, searched before the global one. |
| Omit trace actions | Leaves trace calls out. |
| Protect from import, Permit debugging | Mark the SWF, with optional passwords. |
| Compress movie | Compresses the SWF file with zlib. |
| Local playback security | Flash 8: whether a local SWF may read local files or the network. |
Compile errors
Flash reports errors in the Output panel, from Flash CS3 in a Compiler Errors panel, with the file or the scene, layer and frame, and the line. Typical errors, in paraphrase:
- a type mismatch, naming the type found and the type required;
- no property or method of that name in the class;
- a class or interface that could not be loaded, because no file on the classpath matches;
- a class that must be defined in a file whose path matches its name;
- a member used against its
privateorstaticdeclaration; - a class that does not implement every method of an interface it names;
- syntax errors, as in ActionScript 1.0.
The checks end there. Anything the annotations do not cover, untyped values, bracket access and timeline paths, is left to fail or succeed when the movie runs.
What a class becomes
For each class it compiles, Flash adds a movie clip symbol to the SWF named
__Packages. and the class’s full name, such as
__Packages.com.example.Volume, exported but never placed. The class code
goes in a DoInitAction tag for that symbol, in the export frame, so it runs
once before that frame’s scripts, superclasses before subclasses. In outline, as a
decompiler shows it, the code is:
if (!_global.com) { _global.com = new Object(); }
if (!_global.com.example) { _global.com.example = new Object(); }
if (!_global.com.example.Volume) {
var v1 = function () { … }; // the constructor
com.example.Volume = v1;
com.example.Volume extends MovieClip; // only if the class extends one
var v2 = v1.prototype;
v2.setLevel = function (n) { … }; // each method
v2.__get__percent = function () { … }; // get percent
v2.__set__percent = function (value) { … };
v2.addProperty("percent", v2.__get__percent, v2.__set__percent);
ASSetPropFlags(com.example.Volume.prototype, null, 1);
}
for..in.- The guard
if (!_global…)means a class already defined, for example by a movie loaded earlier, is not replaced. - Methods and starting values of variables go on the prototype; static members on the constructor.
- A getter without a setter is given an empty function as its setter. Where the
compiler knows an object’s type, it may call
__get__name()directly instead of reading the property. - The
extendsline is theExtendsaction of SWF 7, which links the prototypes and records the superclass constructor forsuper. - A symbol whose linkage names an ActionScript 2.0 class gets its own
DoInitActionwithObject.registerClass("linkage name", com.example.Ball). - Types,
publicandprivateare gone; names of members and classes remain as strings.
Flash Player 7 actions
Publishing for Flash Player 7 or later lets the compiler use the actions SWF 7 added for this language:
| Action | Used for |
|---|---|
DefineFunction2 | Every function and method: locals, parameters, this, arguments, super, _root, _parent and _global kept in registers where possible, which is faster |
Extends | class … extends |
ImplementsOp | implements |
CastOp | Casts to classes and interfaces, giving null on failure |
Try, Throw | try, catch, finally and throw |
For Flash Player 6 these actions do not exist: functions use the older
DefineFunction, and exceptions cannot be used. The actions and the tags are
described in ActionScript in SWF, and where scripts are
kept in the document on the FLA pages.