33
Chapter 4: ActionScript language
and syntax
ActionScript 3.0 comprises both the core ActionScript language and the Adobe Flash Player Application
Programming Interface (API). The core language is the part of ActionScript that implements the draft ECMAScript
(ECMA-262), Edition 4 draft language specification. The Flash Player API provides programmatic access to Flash
Player.
This chapter provides a brief introduction to the core ActionScript language and syntax. After reading this chapter,
you should have a basic understanding of how to work with data types and variables, how to use proper syntax, and
how to control the flow of data in your program.
Contents
Language overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Objects and classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Packages and namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Data types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Looping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Language overview
Objects lie at the heart of the ActionScript 3.0 language—they are its fundamental building blocks. Every variable
you declare, every function you write, and every class instance you create is an object. You can think of an Action-
Script 3.0 program as a group of objects that carry out tasks, respond to events, and communicate with one another.
Programmers familiar with object-oriented programming (OOP) in Java or C++ may think of objects as modules
that contain two kinds of members: data stored in member variables or properties, and behavior accessible through
methods. The ECMAScript edition 4 draft, the standard upon which ActionScript 3.0 is based, defines objects in a
similar but slightly different way. In the ECMAScript draft, objects are simply collections of properties. These
properties are containers that can hold not only data, but also functions or other objects. If a function is attached to
an object in this way, it is called a method.
While the ECMAScript draft definition may seem a little odd to programmers with a Java or C++ background, in
practice, defining object types with ActionScript 3.0 classes is very similar to the way classes are defined in Java or
C++. The distinction between the two definitions of object is important when discussing the ActionScript object
model and other advanced topics, but in most other situations the term properties means class member variables as
opposed to methods. The ActionScript 3.0 Language and Components Reference, for example, uses the term properties
to mean variables or getter-setter properties. It uses the term methods to mean functions that are part of a class.

Get ADOBE® FLEX® 3: PROGRAMMING ACTIONSCRIPT™ 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.