O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

ActionScript Overview

ActionScript 3.0 is an object-oriented language for creating applications and scripted multimedia content for playback in Flash client runtimes (such as Flash Player and Adobe AIR). With a syntax reminiscent of Java and C#, ActionScript's core language should be familiar to experienced programmers. For example, the following code creates a variable named width, of type int (meaning integer), and assigns it the value 25:

var width:int = 25;

The following code creates a for loop that counts up to 10:

for (var i:int = 1; i <= 10; i++) {
  // Code here runs 10 times
}

And the following code creates a class named Product:

// The class definition
public class Product {
  // An instance variable of type Number
  var price:Number;

  // The Product class constructor method
  public function Product () {
    // Code here initializes Product instances
  }

  // An instance method
  public function doSomething ():void {
    // Code here executes when doSomething() is invoked
  }
}

The Core Language

ActionScript 3.0's core language is based on the ECMAScript 4th edition language specification, which is still under development as of May 2007.

Tip

The ECMAScript 4 specification can be viewed at http://developer.mozilla.org/es4/spec/spec.html. The ActionScript 3.0 specification can be viewed at http://livedocs.macromedia.com/specs/actionscript/3.

In the future, ActionScript is expected to be a fully conforming implementation of ECMAScript 4. Like ActionScript, the popular web browser language JavaScript is also based on ECMAScript. The future Firefox 3.0 web browser is expected to implement JavaScript 2.0 using the same code base as ActionScript, which was contributed to the Mozilla Foundation by Adobe in November 2006 (for information, see http://www.mozilla.org/projects/tamarin).

ECMAScript 4 dictates ActionScript's basic syntax and grammar—the code used to create things such as expressions, statements, variables, functions, classes, and objects. ECMAScript 4 also defines a small set of built-in datatypes for working with common values (such as String, Number, and Boolean).

Some of ActionScript 3.0's key core-language features include:

  • First-class support for common object-oriented constructs, such as classes, objects, and interfaces

  • Single-threaded execution model

  • Runtime type-checking

  • Optional compile-time type-checking

  • Dynamic features such as runtime creation of new constructor functions and variables

  • Runtime exceptions

  • Direct support for XML as a built-in datatype

  • Packages for organizing code libraries

  • Namespaces for qualifying identifiers

  • Regular expressions

All Flash client runtimes that support ActionScript 3.0 share the features of the core language in common. This book covers the core language in its entirety, save for regular expressions.

Flash Runtime Clients

ActionScript programs can be executed in three different client runtime environments: Adobe AIR, Flash Player, and Flash Lite.

Adobe AIR

Adobe AIR runs Flash-platform applications intended for desktop deployment. Adobe AIR supports SWF-format content, as well as content produced with HTML and JavaScript. Adobe AIR must be installed directly on the end user's computer at the operating-system level.

For more information, see http://www.adobe.com/go/air.

Flash Player

Flash Player runs Flash-platform content and applications intended for web deployment. Flash Player is the runtime of choice for embedding SWF-format content on a web page. Flash Player is typically installed as a web browser add-on but can also run in standalone mode.

Flash Lite

Flash Lite runs Flash-platform content and applications intended for mobile-device deployment. Due to the performance limitations of mobile devices, Flash Lite typically lags behind Flash Player and Adobe AIR in both speed and feature set. As of June 2007, Flash Lite does not yet support ActionScript 3.0.

The preceding Flash client runtimes offer a common core set of functionality, plus a custom set of features that cater to the capabilities and security requirements of the runtime environment. For example, Adobe AIR, Flash Player, and Flash Lite all use the same syntax for creating a variable, but Adobe AIR includes window-management and filesystem APIs, Flash Lite can make a phone vibrate, and Flash Player imposes special web-centric security restrictions to protect the end user's privacy.

Runtime APIs

Each Flash client runtime offers its own built-in set of functions, variables, classes, and objects—known as its runtime API. Each Flash client runtime's API has its own name. For example, the Flash client runtime API defined by Flash Player is known as the Flash Player API.

All Flash client runtime APIs share a core set of functionality in common. For example, every Flash client runtime uses the same basic set of classes for displaying content on screen and for dispatching events.

Key features shared by all Flash client runtime APIs include:

  • Graphics and video display

  • A hierarchical event architecture

  • Text display and input

  • Mouse and keyboard control

  • Network operations for loading external data and communicating with server-side applications

  • Audio playback

  • Printing

  • Communicating with external local applications

  • Programming utilities

This book covers the first five of the preceding items. For information on other specific Flash client runtime APIs, consult the appropriate product documentation.

Components

In addition to the Flash client runtime APIs, Adobe also offers two different sets of components for accomplishing common programming tasks and building user interfaces. Flex Builder 2 and the free Flex 2 SDK include the Flex framework, which defines a complete set of user interface controls, such as RadioButton, CheckBox, and List. The Flash authoring tool provides a similar set of user interface components. The Flash authoring tool's components combine code with manually created graphical assets that can be customized by Flash developers and designers.

Both the Flex framework and the Flash authoring tool's component set are written entirely in ActionScript 3.0. The user interface components in the Flex framework generally have more features than those in the Flash authoring tool's component set and, therefore, also have a larger file size.

Warning

User interface components from the Flex framework cannot be used in the Flash authoring tool, but user interface components from the Flash authoring tool can be used (both legally and technically) with Flex Builder 2 and mxmlc.

This book does not cover component use or creation in ActionScript. For information on components, see the appropriate product documentation.

The Flash File Format (SWF)

ActionScript code must be compiled into a .swf file for playback in one of Adobe's Flash client runtimes. A .swf file can include both ActionScript bytecode and embedded assets (graphics, sound, video, and fonts). Some .swf files contain assets only and no code, while others contain code only and no assets. A single ActionScript program might reside entirely within a single .swf file, or it might be broken into multiple .swf files. When a program is broken into multiple .swf files, one specific .swf file provides the program point of entry, and loads the other .swf files as required. Breaking a complex program into multiple .swf files makes it easier to maintain and, for Internet-delivered applications, can give the user faster access to different sections of the program.

ActionScript Development Tools

Adobe offers the following tools for creating ActionScript code:

Adobe Flash

http://www.adobe.com/go/flash/

A visual design and programming tool for creating multimedia content that integrates graphics, video, audio, animation, and interactivity. In Adobe Flash, developers create interactive content by combining ActionScript code with animation, manually created content, and embedded assets. Adobe Flash is also known as the Flash authoring tool. As of June 2007, the latest version of the Flash authoring tool is Flash CS3 (Version 9 of the software).

Adobe Flex Builder

http://www.adobe.com/products/flex/productinfo/overview/

A development tool for producing content using either pure ActionScript or MXML, an XML-based language for describing user interfaces. Flex Builder includes a development framework known as the Flex framework, which provides an extensive set of programming utilities and a library of skinnable, styleable user-interface controls. Based on Eclipse, the popular open source programming tool, Flex Builder 2 can be used in either hand-coding mode or in a visual-development mode similar to Microsoft's Visual Basic.

Adobe Flex 2 SDK

http://www.adobe.com/go/flex2_sdk

A free command-line toolkit for creating content using either pure ActionScript 3.0 or MXML. The Flex 2 SDK includes the Flex framework and a command-line compiler, mxmlc (both of which are also included with Adobe Flex Builder 2). Using the Flex 2 SDK, developers can create content for free in the programming editor of their choice. (For a wide variety of open source tools and utilities for ActionScript development, see http://osflash.org.)

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required