An ActionScript 1.0 .as file is plain text holding code that could equally
have been typed into a frame of a Flash document. It has no header, no signature and no
required structure. It takes part in a movie only when a script in the FLA names it with
#include, and then only when the movie is compiled: the SWF file holds the
compiled code, not a reference to the file.
#include
The directive names one file, in double quotes, and is replaced by the file’s whole text before the script is compiled:
#include "menu.as"
#include "lib/strings.as"
#include "../shared/colours.as"
- It is a compiler directive, not a statement: it starts with
#, stands on a line of its own and takes no semicolon. A semicolon after it is reported as an error. - A path without a folder means a file next to the FLA. A relative path is followed
from there, with
/between folders and..for the folder above. Absolute paths work but tie the FLA to one computer’s folders. - An included file may include others. A file included twice is pasted twice.
- The file is read every time the movie is published or tested, so a change to it needs the movie to be published again. The SWF never reads it.
- A syntax check of the script in Flash also checks the files it includes.
What a file holds
Whatever is legal where it is included. Almost always that is a frame script, so an
.as file holds statements, variable assignments and function
definitions, which run in the scope of the timeline that includes them, as if typed on
that frame. Function definitions in it are available to the whole frame script, even
to code above the #include line, because functions declared with the
function statement are created when the frame’s code starts.
The handlers on() and onClipEvent() belong to the scripts of
button and movie clip instances in the FLA, and are not found in shared
.as files. From Flash MX the same work can be done in a frame script by
assigning functions to onPress, onEnterFrame and the other
handler properties, which an .as file can hold; see
Events.
Common kinds of file were libraries of functions shared between movies, a
constructor and its prototype methods for one kind of object, prototype extensions to
built-in classes such as Array.prototype.shuffle, and settings or text kept
out of the FLA so that someone else could edit them.
#initclip and class files
Flash MX added a second pair of directives for the first frame of a movie clip
symbol. Code between #initclip and #endinitclip runs once,
before the frame scripts of the frame that first uses the symbol, or before frame 1 when
the symbol is exported for ActionScript. It was made for defining a constructor and
registering it with the symbol, so a common pattern kept the constructor in an
.as file named after it:
// Frame 1 of the Slider symbol, in the FLA
#initclip 1
#include "Slider.as"
Object.registerClass("Slider", Slider);
#endinitclip
#initclip block that includes a constructor file and gives
the symbol its class.The optional number after #initclip orders the blocks of several symbols:
lower numbers run first. It must be a plain whole number, not hexadecimal and not a
variable. Each #initclip block is compiled into its own
DoInitAction tag; see Compiler.
The file names Slider.as and the like were a convention only. Nothing in
ActionScript 1.0 ties a file’s name to what it holds.
Text encoding
| Program | How an included file is read |
|---|---|
| Flash 5 | In the system’s code page: Latin-1 (Windows-1252) on Western systems and Shift-JIS on Japanese ones. Flash Player 5 has no Unicode support, so other characters cannot be used. |
| Flash MX | In the system’s code page, unless the file’s first line is the comment //!-- UTF8 and the file is saved as UTF-8; then it is read as UTF-8. Flash Player 6 holds strings as Unicode. |
The comment is written exactly so, with no space after //, and it is an
ordinary comment to anything else that reads the file:
//!-- UTF8
greeting_fr = "Bienvenue à tous";
greeting_ja = "ようこそ";
Flash MX’s Actions panel could not take Unicode characters typed into it, so files like
this, or \u escapes in strings, were the ways to put other scripts into a
movie. Later versions of Flash have a preference for the encoding of
.as files; see the family page.
Writing and naming
Flash 5 and Flash MX had no editor for .as files of their own; the
Actions panel could import a script from a text file and export one to it, and any
text editor did the rest. There are no naming rules. Files are named freely, usually
in lower case for libraries and after the constructor for a file that defines one,
and kept beside the FLA or in a folder below it.