ActionScript 1.0 is compiled by the Flash authoring program itself, when a movie is
published or tested. There is no separate compiler and no command line. The compiler
reads the scripts in the FLA, pastes in any included .as files, and turns
each script into AVM1 bytecode, a list of actions for Flash Player’s stack machine,
stored in the SWF file. Flash 5 writes SWF version 5 files and Flash MX version 6, or
an earlier version chosen in the publish settings.
Directives
| Directive | Effect | Since |
|---|---|---|
#include "file" | Replaced by the file’s text before compiling; see The .as file. | Flash 5 |
#initclip [order] … #endinitclip | The code between them, in a symbol’s first frame, is compiled into its own initialisation block, run once before the symbol is first used; see #initclip. | Flash MX |
Publish settings that affect scripts
| Setting | Effect |
|---|---|
| Version | The Flash Player, and so the SWF version, to publish for. Code for an older player can only use what that player has: for Flash Player 4, only the Flash 4 actions. |
| Omit Trace actions | Leaves every trace call out of the SWF. |
| Protect from import | Marks the SWF so that Flash will not import it, with an optional password. It does not stop decompilers. |
| Debugging permitted | Lets the Flash Debugger connect to the movie, with an optional password. |
| Compress movie | Flash MX, for Flash Player 6: compresses the whole SWF file, scripts included, with zlib. |
| Generate size report | Writes a text report of the bytes each frame and symbol takes, scripts included. |
Where the compiled code goes
Each kind of script becomes a different part of the SWF file. The tags are described in ActionScript in SWF, and every action in AVM1 actions.
| Script in the FLA | Compiled into |
|---|---|
| A frame script, with its includes | A DoAction tag in that frame of the timeline, or of the sprite for a symbol’s timeline |
on() on a button | The condition actions of the button’s DefineButton2 tag, one record per on block, with the events as flags and keys as codes |
onClipEvent() on a clip, and from Flash MX on() on a clip | The clip actions of the PlaceObject2 tag that places that instance, one record per block, with the events as flags |
#initclip block | A DoInitAction tag (SWF 6) naming the symbol it belongs to |
The source text is not kept. Comments, white space and layout are lost; names of variables, properties and functions survive as strings, because the player looks them up by name when the code runs. A decompiler can therefore rebuild readable code, but not the original text.
A worked example
Two statements of a frame script, and the actions the compiler makes from them, with their names from the SWF specification:
total = price * count;
trace("Total: " + total);
ConstantPool "total" "price" "count" "Total: "
Push "total" name to assign, from the pool
Push "price"
GetVariable reads price
Push "count"
GetVariable reads count
Multiply
SetVariable total = price * count
Push "Total: "
Push "total"
GetVariable
Add2 ECMAScript +: joins, since one side is a string
Trace
The stack machine pushes values, and each action takes its operands from the top of
the stack. Strings and names are stored once in a constant pool, from Flash 5, and
referred to by number. Flash 5 added the actions for ECMAScript’s typed operators,
such as Add2, Equals2 and Less2, beside Flash 4’s
numeric Add, Equals and Less, and the actions
for objects, methods and functions, such as NewObject,
CallMethod, GetMember and DefineFunction. Flash MX
added InstanceOf, StrictEquals, Greater,
StringGreater and Enumerate2. The full list is on the SWF
pages.
Errors
The compiler reports syntax errors in the Output panel when publishing or testing,
giving the scene, layer and frame, or the file, and the line. It checks syntax only: a misspelt name, a wrong argument or
a missing method is not an error until the code runs, and then usually fails quietly,
giving undefined.