Learning ActionScript 3 - Adobe Inc.

Learning

ACTIONSCRIPT? 3.0

Legal notices

Legal notices

For legal notices, see .

Last updated 5/2/2011

iii

Contents

Chapter 1: Introduction to ActionScript 3.0

About ActionScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Advantages of ActionScript 3.0

........................................................................................ 1

What¡¯s new in ActionScript 3.0

......................................................................................... 2

Chapter 2: Getting started with ActionScript

Programming fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Working with objects

.................................................................................................. 7

Common program elements

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

Example: Animation portfolio piece (Flash Professional)

Building applications with ActionScript

Creating your own classes

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Example: Creating a basic application

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

Chapter 3: ActionScript language and syntax

Language overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

Objects and classes

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

Packages and namespaces

Variables

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

Data types

Syntax

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

Operators

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

Conditionals

Looping

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

Functions

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

Chapter 4: Object-oriented programming in ActionScript

Introduction to object-oriented programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

Classes

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

Interfaces

Inheritance

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

Advanced topics

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

Example: GeometricShapes

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

Last updated 5/2/2011

1

Chapter 1: Introduction to ActionScript

3.0

About ActionScript

ActionScript is the programming language for the Adobe? Flash? Player and Adobe? AIR? run-time environments. It

enables interactivity, data handling, and much more in Flash, Flex, and AIR content and applications.

ActionScript executes in the ActionScript Virtual Machine (AVM), which is part of Flash Player and AIR. ActionScript

code is typically transformed into bytecode format by a compiler. (Bytecode is a type of programming language that¡¯s

written and understood by computers.) Examples of compilers include the one built in to Adobe? Flash? Professional

and the one that is built in to Adobe? Flash? Builder? and available in the Adobe? Flex? SDK. The bytecode is embedded

in SWF files, which Flash Player and AIR execute.

ActionScript 3.0 offers a robust programming model that is familiar to developers with a basic knowledge of objectoriented programming. Some of the key features of ActionScript 3.0 that improve over previous ActionScript versions

include the following:

? A new ActionScript Virtual Machine, called AVM2, that uses a new bytecode instruction set and provides

significant performance improvements

? A more modern compiler code base that performs deeper optimizations than previous versions of the compiler

? An expanded and improved application programming interface (API), with low-level control of objects and a true

object-oriented model

? An XML API based on the ECMAScript for XML (E4X) specification (ECMA-357 edition 2). E4X is a language

extension to ECMAScript that adds XML as a native data type of the language.

? An event model based on the Document Object Model (DOM) Level 3 Events Specification

Advantages of ActionScript 3.0

ActionScript 3.0 goes beyond the scripting capabilities of previous versions of ActionScript. It is designed to facilitate

the creation of highly complex applications with large data sets and object-oriented, reusable code bases. ActionScript

3.0 is not required for content that runs in Adobe Flash Player. However, it opens the door to performance

improvements that are only available with the AVM2 (the ActionScript 3.0 virtual machine). ActionScript 3.0 code can

execute up to ten times faster than legacy ActionScript code.

The previous version of ActionScript Virtual Machine, AVM1, executes ActionScript 1.0 and ActionScript 2.0 code.

Flash Player 9 and 10 support AVM1 for backward compatibility.

Last updated 5/2/2011

2

LEARNING ACTIONSCRIPT 3.0

Introduction to ActionScript 3.0

What¡¯s new in ActionScript 3.0

ActionScript 3.0 contains many classes and features that are similar to ActionScript 1.0 and 2.0. However, ActionScript

3.0 is architecturally and conceptually different from previous versions of ActionScript. The enhancements in

ActionScript 3.0 include new features of the core language and an improved API that provides increased control of

low-level objects.

Core language features

The core language defines the basic building blocks of the programming language, such as statements, expressions,

conditions, loops, and types. ActionScript 3.0 contains many features that speed up the development process.

Run-time exceptions

ActionScript 3.0 reports more error conditions than previous versions of ActionScript. Run-time exceptions are used

for common error conditions, improving the debugging experience and enabling you to develop applications that

handle errors robustly. Run-time errors can provide stack traces annotated with source file and line number

information, helping you quickly pinpoint errors.

Run-time types

In ActionScript 3.0, type information is preserved at run time. This information is used to perform run-time type

checking, improving the system¡¯s type safety. Type information is also used to represent variables in native machine

representations, which improves performance and reduces memory usage. By way of comparison, in ActionScript 2.0

type annotations are primarily a developer aid and all values are dynamically typed at run time.

Sealed classes

ActionScript 3.0 includes the concept of sealed classes. A sealed class possesses only the fixed set of properties and

methods that are defined at compile time; additional properties and methods cannot be added. The inability of

changing a class at run time enables stricter compile-time checking, resulting in more robust programs. It also

improves memory usage by not requiring an internal hash table for each object instance. Dynamic classes are also

possible using the dynamic keyword. All classes in ActionScript 3.0 are sealed by default, but can be declared to be

dynamic with the dynamic keyword.

Method closures

ActionScript 3.0 enables a method closure to automatically remember its original object instance. This feature is useful

for event handling. In ActionScript 2.0, method closures do not remember what object instance they were extracted

from, leading to unexpected behavior when the method closure is called.

ECMAScript for XML (E4X)

ActionScript 3.0 implements ECMAScript for XML (E4X), recently standardized as ECMA-357. E4X offers a natural,

fluent set of language constructs for manipulating XML. In contrast to traditional XML-parsing APIs, XML with E4X

performs like a native data type of the language. E4X streamlines the development of applications that manipulate

XML by drastically reducing the amount of code needed.

To view the ECMA E4X specification, go to ecma-.

Regular expressions

ActionScript 3.0 includes native support for regular expressions so that you can quickly search for and manipulate

strings. ActionScript 3.0 implements support for regular expressions as they are defined in the ECMAScript (ECMA262) edition 3 language specification.

Last updated 5/2/2011

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download