An .as file is never run as text. A compiler reads it with every class
it uses, checks it and writes ABC bytecode into a SWF file or a SWC library. This page
covers the compilers, how they find and read files, and the directives that act at
compile time.
The compilers
| Compiler | Where | Makes |
|---|---|---|
| Flash authoring | Flash CS3 Professional (2007) to Adobe Animate | A SWF from a .fla document: its frame scripts, its document class and every class they use. |
mxmlc | Flex SDK, Apache Flex SDK, AIR SDK | A SWF application from one main .as or .mxml file and everything it reaches. |
compc | The same SDKs | A SWC library from a set of classes. |
| ActionScript Compiler 2.0 | AIR SDK, from AIR 3.4 and Flash Builder 4.7 in 2012 | A rewritten compiler behind the AIR SDK’s mxmlc and compc, for projects written in ActionScript only; the Flex framework still uses the older compiler. |
adt | AIR SDK | Not a compiler: packages a compiled SWF into an AIR, APK, IPA or desktop installer. |
mxmlc -source-path=src -library-path+=libs -output=bin/Main.swf src/com/example/Main.as
compc -source-path=src -include-sources=src/com/example/util -output=bin/util.swc
Only classes that are reached from the main class, or listed for inclusion, end up
in the SWF. A class that is only named in a string, such as one created with
getDefinitionByName, must be referenced somewhere or included with
-includes.
The source path
The compiler finds a class by turning its full name into a path under each folder of
the source path: com.example.Main is com/example/Main.as.
mxmlc takes -source-path (+= adds to the list)
and adds the folder of the main file. Flash authoring uses the folder of the
.fla, the document’s own source path and the application-wide one in its
preferences. Compiled libraries come from the library path: SWC files with
-library-path, or -external-library-path for ones the
runtime will supply.
A class must sit in the file and folder its name gives; the compiler reports an error when the package or class name and the path disagree.
File encoding
The Flex and AIR compilers use the byte order mark when a file has one. Without it,
they read the file in the encoding named by -actionscript-file-encoding,
or else in the computer’s default encoding. MXML files are not affected, since their
XML declaration gives the encoding. Saving every .as file as UTF-8 avoids
any difference between computers.
The include directive
include "path/file.as";
Pastes the text of another file in place of the directive when the code is compiled,
usually by a path relative to the file that holds the directive. The pasted text must
make sense where it lands: a list of methods inside a class body, statements inside a
function. After the included file changes, every SWF that uses it must be compiled
again. include does not import classes. ActionScript 1.0 and 2.0 spelt it
#include.
package com.example {
import flash.display.Sprite;
public class Player extends Sprite {
include "PlayerMovement.as"; // methods shared with another class
}
}
Conditional compilation
Constants defined outside the code, in the form namespace::name, are
known to the compiler and can switch code on and off. CONFIG is the usual
namespace, but any name works.
mxmlc -define=CONFIG::debug,true -define+=CONFIG::version,"'1.2'" …
package {
import flash.display.Sprite;
import com.example.DebugPanel;
public class Main extends Sprite {
public function Main() {
CONFIG::debug {
trace("built as version", CONFIG::version);
showPanel();
}
}
CONFIG::debug
private function showPanel():void {
addChild(new DebugPanel());
}
}
}
- A Boolean constant before a block, or before a single definition (
CONFIG::debug public function …), keeps it when true and drops it entirely when false, like#ifdefin C. It works on definitions of classes, functions and variables and on blocks of statements. - Constants cannot switch metadata tags or
importlines on and off, according to Adobe’s documentation. - String, number and Boolean constants can be used as values anywhere an expression goes; the compiler puts the value in.
- A value can be an expression the compiler can work out:
"CONFIG::debug && !CONFIG::release","4 - 1". - String values need two sets of quotes on the command line,
"'text'", and one in a configuration file:<value>'text'</value>. - A definition on the command line overrides the same one in a configuration file;
-define+=adds to the list and-define=replaces it.
Flash authoring takes the same constants in the ActionScript settings of the publish settings.
The document class and linked classes
A Flash document names one class as its document class. It extends
MovieClip, or Sprite when the main timeline needs no frames
of its own, and becomes the main timeline: the player creates it first, as the first child
of the Stage. Without a document class, Flash generates one, named
MainTimeline in a package named after the document (such as
Untitled_fla), to hold the frame scripts.
A library symbol exported for ActionScript gets a class name and a base class in its
linkage properties. When a .as file of that name exists on the source
path, the symbol uses that class; otherwise Flash generates an empty class extending
the base class. Instances placed on the stage with instance names become properties
of the timeline’s class: with “Automatically declare stage instances” on (the
default), Flash declares them as public variables; with it off, the class must declare
them itself as public var.
Where Flash keeps these names and the frame scripts inside the .fla is
described on the FLA pages. In the compiled SWF a
SymbolClass tag links each symbol, and the main timeline as character 0,
to its class; see Compiled bytecode.
Common compiler options
These are the mxmlc and compc names; Flash authoring has
most of them as publish settings. Options can also be written in a configuration
file, such as the SDK’s flex-config.xml or one given with
-load-config.
| Option | Default | Meaning |
|---|---|---|
-source-path | The main file’s folder | Folders searched for .as and .mxml files. |
-library-path, -external-library-path | The SDK’s libraries | SWC files to link in, or to compile against without linking. |
-output | Next to the main file | The SWF or SWC to write. |
-strict | true | Strict mode: compile-time type checking. See Strict and standard mode. |
-warnings | true | Report warnings. |
-define | – | A conditional compilation constant. |
-debug | false for mxmlc | Keep file names and line numbers for the debugger and stack traces. |
-optimize | true | Optimise the bytecode. |
-default-size, -default-frame-rate, -default-background-color | –, 24, – | The stage size, frame rate and colour, unless [SWF] sets them. |
-target-player | Depends on the SDK | The lowest player version to compile for, as major.minor.revision. |
-swf-version | Depends on the SDK | The SWF file version to write, which is not the player version (SWF 14 goes with Flash Player 11.1). |
-keep-as3-metadata | Bindable, Managed, ChangeEvent, NonCommittingChangeEvent, Transient | Metadata tags to keep in the bytecode. |
-actionscript-file-encoding | The computer’s default | The encoding of .as files without a byte order mark. |
-as3, -es | true, false | The class-based object model with the AS3 namespace open, or the ECMAScript prototype model (which needs -as3=false and -strict=false). |
-frame | – | Puts classes on a further frame under a label, as [Frame] does. |
-includes, -include-libraries | – | Link classes, or whole libraries, even if nothing refers to them. |
SWC libraries
A SWC is a zip archive holding library.swf, the compiled classes, and
catalog.xml, which lists every definition, what it depends on and the
SWF’s digest. It may also hold assets, ASDoc and a manifest. A SWC is linked like
source but is not recompiled; Flash authoring and the SDK compilers both use them.