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

Classes and Objects

Imagine you are going to build an airplane, entirely from scratch. Think about the process you would follow. You very likely wouldn't just head to a metal shop and start welding. You'd have to draw up a blueprint for the airplane first. In fact, given that you are building the airplane from scratch, you'd have to draw up not just one, but many blueprints—one for each of the airplane's many parts (the wheels, the wings, the seats, the brakes, and so on). Each blueprint would describe a specific part conceptually and correspond to an actual part in the physical incarnation of the airplane. To build the airplane, you would manufacture each of the parts individually, and then assemble them according to a master blueprint. The interoperation of the airplane's assembled parts would produce the airplane's behavior.

If that all sounds logical to you, you've got what it takes to become an ActionScript programmer. Just as an airplane flying through the sky is a group of interoperating parts based on a set of blueprints, a running ActionScript program is a group of interoperating objects, based on a set of classes. ActionScript objects represent both the tangible things and the intangible concepts in a program. For example, an object might represent a number in a calculation, a clickable button in a user interface, a point in time on a calendar, or a blur effect on an image. Objects are incarnations, or instances, of classes. Classes are the blueprints upon which objects are based.

The first step in writing a new program is determining its classes. Each class describes, in code, both the characteristics and behavior of a particular type of object. Some of the classes in a program must be written from scratch, while others are provided by ActionScript and the various Flash runtimes. Classes written from scratch (known as custom classes) are used to produce specialized types of content, such as an order form for a shopping application, a car in a racing game, or a message in a chat application. By contrast, classes provided by ActionScript and the various Flash runtimes (known as built-in classes) are used to perform fundamental tasks such as creating numbers and text, playing sounds, displaying images, accessing the network, and responding to user input.

From the classes in a program, we make (or instantiate) objects and then tell those objects what to do. What the objects do determines the behavior of the program.


Building a program with classes and objects is known as object-oriented programming (OOP).

In the next section we'll start writing an actual program, but before we do, let's take a brief look at an important group of classes, known as native classes, that are built directly into ActionScript. The native classes, listed in Table 1-1, are used to manipulate basic types of information, such as numbers and text. You can expect to use instances of at least one or two of the native classes in every program you write—much like you might use ready-made parts from a third-party supplier when building an airplane. Read over Table 1-1 for basic familiarity. In the coming chapters, we'll study the native classes in much more detail.

Table 1-1. ActionScript's native classes




Represents textual data (i.e., a string of characters)


Represents the logical states true and false


Represents floating-point numbers (i.e., numbers with a fractional value)


Represents integer numbers (i.e., numbers with no fractional value)


Represents positive integer numbers


Represents an ordered list


Represents a program error (i.e., a problem in your code)


Represents a specific point in time


Contains common mathematical values and operations


Defines tools for searching and replacing text


Represents a reusable set of instructions that can be executed, or called, repeatedly


Defines the basic features of every object in ActionScript

Now let's try using classes and objects in an example program—a simple simulated zoo game with virtual pets.


Using the technique known as timeline scripting in the Flash authoring tool, it is possible to create an ActionScript program without first creating a class (see Chapter 29). However, even if you never expect to create classes yourself, you should still study the techniques presented in this chapter. Knowing how classes are created will greatly deepen your understanding of ActionScript and make you a better programmer.

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