Introduction
An .as file holds ActionScript source code as plain text. ActionScript
3.0, the version on this page, arrived in June 2006 with Flash Player 9 and Flex 2, and
reached the Flash authoring tool with Flash CS3 Professional in 2007. It is a different
language from ActionScript 1.0 and 2.0, and it runs on a different virtual machine,
the ActionScript Virtual Machine 2 (AVM2). Flash Player 9 and later carry both machines:
AVM1 for older content and AVM2 for ActionScript 3.0.
The language was built on the drafts of ECMAScript 4, the planned fourth edition of the standard behind JavaScript. It has classes, packages, namespaces, optional type annotations checked by the compiler, sealed objects, method closures, native XML (E4X) and regular expressions. ECMAScript 4 itself was abandoned in 2008, so ActionScript 3.0 kept its own course. Adobe gave the source of the AVM2 to the Mozilla Foundation in November 2006 as the Tamarin project; that code is still published as avmplus.
Flash Player was discontinued at the end of 2020, but ActionScript 3.0 is still written today. Adobe Animate, the former Flash Professional, still makes ActionScript 3.0 documents. Adobe AIR, which packages ActionScript 3.0 applications for Windows, macOS, Android and iOS, passed from Adobe to HARMAN in 2019, and its SDK is now published from a Wipro site. The Apache Flex SDK, the open-source continuation of Adobe Flex, holds the command-line compilers; its last release was 4.16.1, in November 2017. Ruffle, a Flash Player emulator written in Rust, runs compiled ActionScript 3.0 in browsers through its own AVM2.
An .as file dropped on any page opens in the explorer, which shows it as
text. The explorer recognises ActionScript 3.0 by its package block.
Structure
A typical ActionScript 3.0 file holds one package block. Inside it come
the import directives and then exactly one definition that other files can
see: a class, an interface, a function, a variable, a constant or a namespace. That
definition must be public or internal and must have the same
name as the file, so com.example.Hello lives in
com/example/Hello.as, with one folder for each part of the package
name. A package with no name, package { … }, is the top-level package, for
files at the root of the source path.
package com.example {
import flash.display.Sprite;
import flash.text.TextField;
[SWF(width="400", height="300", frameRate="30", backgroundColor="#FFFFFF")]
public class Hello extends Sprite {
public function Hello() {
var label:TextField = new TextField();
label.text = "Hello, world";
addChild(label);
}
}
}
com/example/Hello.as: one package, its
imports and one public class, named after the file.Helper definitions
Further classes, functions and variables may follow the closing brace of the package
block. They are visible only to code in the same file, which is the language’s way
of writing a private helper class. Imports inside the package block do not reach
them, so they need their own import lines.
package {
public class Main {
public function Main() {
var s:Stack = new Stack();
s.push(1);
}
}
}
import flash.utils.getTimer;
class Stack {
private var items:Array = [];
public var created:int = getTimer();
public function push(value:*):void { items.push(value); }
}
Main.as, with a helper class that no other file can
see.Encoding
The file is plain text. The Flex and AIR compilers use a byte order mark when there
is one; without it they read the file in the computer’s default encoding unless the
actionscript-file-encoding option names another. UTF-8, with or without
a byte order mark, is the usual choice and the safe one. Strings are UTF-16 at run
time, and a \u escape gives any character in a string literal.
Source path and the file name
The compiler finds a class by turning its package into folders under each entry of
the source path (also called the classpath or build path). Flash authoring adds the
folder of the .fla file; the mxmlc and compc
compilers take -source-path. Every class used must be imported, unlike in
ActionScript 2.0. See Packages and
namespaces and Compiling.
What else can appear in the file
- The include directive –
include "file.as"pastes another file’s text in at compile time (no#, unlike ActionScript 1.0 and 2.0) - Metadata tags – bracketed instructions to the compiler, such as
[SWF],[Embed]and[Event], placed before a class, property or method - Conditional compilation –
CONFIG::debugconstants that keep or drop whole definitions and blocks - The document class – the class a Flash document names as its main timeline, written as an ordinary
.asfile extendingSpriteorMovieClip
Where ActionScript 3.0 is stored and compiled
ActionScript 3.0 code turns up in three places: as text in .as files, as
text inside Flash documents, and as compiled bytecode inside SWF files.
| Where | Form | What it holds |
|---|---|---|
.as files | Plain text | Classes and interfaces on the source path, and fragments pulled in by include. This page. |
| .fla documents | Text inside the document | Frame scripts on timeline keyframes, the name of the document class, and the class names given to library symbols for export: UTF-16 text in a CS3 or CS4 document, <script> elements in the XML of a CS5 and later one. The FLA pages describe exactly where and how. |
| .swf files | ABC bytecode | Every compiled class, function and frame script, in DoABC tags; a SymbolClass tag links library symbols and the main timeline to their classes. |
.swc libraries | A zip archive | A compiled library.swf and a catalog.xml listing its definitions, used to link libraries without their source. |
Compiling does not keep the source. Each class becomes an entry in the ABC file’s
instance and class tables, each method a method body of AVM2 instructions, and the
package, the access attributes and the namespaces become namespaces on each name. A SWF
whose FileAttributes tag sets its ActionScript 3.0 flag is run by the AVM2;
otherwise the player uses AVM1. See Compiled
bytecode.
Telling it apart
All three ActionScript versions use the .as extension and plain text, so
the content decides:
- ActionScript 3.0 – a
packageblock wraps the definitions:package com.example { public class Hello … } - ActionScript 2.0 – a top-level
classorinterfacewith the full dotted name and no package block:class com.example.Hello { … } - ActionScript 1.0 – loose statements and functions, no
classkeyword, often pulled in with#include
A file holding only a fragment for include has no package block of its
own, so it can look like older code; its import lines, typed
var declarations such as :int and :uint, and
ActionScript 3.0 class names such as flash.display.Sprite show what it
is.
Variants
- ActionScript 1.0 – loose code pulled in by
#include, with prototype-based objects (Flash 5 and Flash MX) - ActionScript 2.0 – one class or interface per file, found through the classpath, compiled for AVM1 (Flash MX 2004 to Flash CS4)
- ActionScript 3.0 – a package block with one public definition, compiled for the AVM2 (Flash CS3, Flex 2 and later)
Links
Software
- Adobe Animate – Adobe’s animation tool, formerly Flash Professional, which edits and compiles ActionScript 3.0 (Windows, macOS)
- AIR SDK – the AIR runtime and compilers for ActionScript 3.0 desktop and mobile applications, with its documentation (Windows, macOS, Linux)
- Apache Flex SDK – free SDK with the
mxmlcandcompccompilers and the Flex framework (Windows, macOS, Linux) - FlashDevelop – free code editor for ActionScript 3.0 projects (Windows)
- IntelliJ IDEA – JetBrains’ development environment, whose Flash/Flex plugin handles ActionScript 3.0 (Windows, macOS, Linux)
- Visual Studio Code – free code editor; the ActionScript & MXML extension adds ActionScript 3.0 support (Windows, macOS, Linux)
- JPEXS Free Flash Decompiler – free tool that turns the bytecode in SWF files back into ActionScript 3.0 source (Windows, macOS, Linux)
Developers
- Ruffle – Flash Player emulator with its own AVM2, which runs compiled ActionScript 3.0 (Rust)
- avmplus – Adobe’s published source of the AVM2 virtual machine, with notes on the ABC format (C++)
- Apache Royale – compiler and framework that turn ActionScript 3.0 into JavaScript
Reference
- ActionScript 3.0 Reference for the Adobe Flash Platform – Adobe’s reference for every class, operator, statement and directive
- Learning ActionScript 3.0 – Adobe’s guide to the language: syntax, types, packages, classes
- ActionScript 3.0 Developer’s Guide – Adobe’s guide to the API: display list, events, XML, regular expressions
- ActionScript 3.0 language specification – Adobe’s specification of the language, as archived
- ActionScript Virtual Machine 2 (AVM2) Overview – Adobe’s May 2007 description of the AVM2 and the ABC format, as archived (PDF)
- Flex compilers – Apache Flex’s guide to
mxmlc,compcand their options