An ActionScript 2.0 .as file is plain text that defines exactly one class
or one interface. The file has no header or signature; its name and its place in the
folders say what it defines, and the compiler relies on both to find it.
Layout of a file
- Optional comments, such as a copyright block.
- Optional
importstatements for the other classes the file names; see Packages. - One declaration:
classorinterface, perhaps withdynamicorintrinsicbeforeclass, the full dotted name, and a body in braces.
Nothing else is allowed outside the declaration: no statements, functions or
variables. Inside it are only member declarations, variables and methods, not loose
statements. #include cannot be used in a class file.
/* com/example/shapes/IShape.as */
interface com.example.shapes.IShape {
function area():Number;
function draw(target:MovieClip):Void;
}
/* com/example/shapes/Circle.as */
import com.example.shapes.IShape;
class com.example.shapes.Circle implements IShape {
private var radius:Number;
public function Circle(r:Number) {
radius = r;
}
public function area():Number {
return Math.PI * radius * radius;
}
public function draw(target:MovieClip):Void {
target.lineStyle(1, 0x000000, 100);
target.moveTo(radius, 0);
// … curveTo calls round the circle
}
}
File name and folder
- The file is named after the class or interface, with the same capitals, plus
.as: classCircleisCircle.as. - The parts of the dotted name before it are folders below a classpath folder:
com.example.shapes.Circleiscom/example/shapes/Circle.as. Folder names become object names when the movie runs, so they must be valid identifiers. - A class in no package sits directly in a classpath folder, often the folder of the FLA.
- The compiler reports an error when the name in the file does not match its path, saying in which file the class needs to be defined.
- There is one class per file. Helper classes go in files of their own.
How the compiler finds the file
No #include is needed. When a script names a class, as a type, with
new, through an import or as the class of a symbol, the
compiler looks for the matching file in each classpath folder in turn, compiles it,
and does the same for every class that file names. Classes nobody refers to are left
out of the SWF. The search order and the cache of compiled classes, the ASO files, are
described on Packages and the classpath.
Because the file is read only when compiling, a changed class needs the movie to be published again; the SWF holds the compiled class, not a link to the file.
Other .as files of the time
- Include files. Loose code pulled into FLA scripts with
#includewas still common, now often with type annotations. Such a file is not a class file: it is the kind described under ActionScript 1.0, compiled with the rules of ActionScript 2.0 when the FLA is set to it. - Intrinsic files. Flash’s own configuration folder holds
.asfiles that declare the built-in classes, such asMovieClip, asintrinsic class, so that the compiler knows their members and types. They hold no code; see intrinsic. - Framework source. Classes such as
mx.utils.Delegate,mx.events.EventDispatcher,mx.transitions.Tweenand the version 2 components ship with Flash as ordinary class files and are compiled into each movie that uses them.
Text encoding
Flash MX 2004 brought Unicode to ActionScript, so a script may hold text in any
language. Its ActionScript preferences choose the encoding of .as files,
separately for opening and importing and for saving and exporting:
| Setting | Means |
|---|---|
| UTF-8 | The file is read or written as UTF-8. |
| Default Encoding | The file is read or written in the system’s traditional code page, such as Windows-1252 on Western Windows or Shift-JIS on Japanese systems. |
A warnings preference reports encoding conflicts when exporting a script whose characters the chosen encoding cannot hold. If text looks wrong after opening a file, the import setting does not match the file; the file itself does not say which encoding it uses.
Writing class files
Flash MX Professional 2004 and later versions have a Script window that edits
.as files directly, with syntax checking against the classpath. Many
developers used outside editors instead, such as FlashDevelop or Eclipse with an
ActionScript plug-in, and compiled with Flash or with MTASC.