Object-oriented ActionScript - Key Object-Oriented Programming Concepts
(Page 2 of 7 )
An object is a self-contained software module that contains related functions (called its methods) and variables (called its properties). Individual objects are created from classes, which provide the blueprint for an object’s methods and properties. That is, a class is the template from which an object is made. Classes can represent theoretical concepts, such as a timer, or physical entities in a program, such as a pull-down menu or a spaceship. A single class can be used to generate any number of objects, each with the same general structure, somewhat as a single recipe can be used to bake any number of muffins. For example, an OOP space fighting game might have 20 individual SpaceShip objects on screen at one time, all created from a single SpaceShip class. Similarly, the game might have one 2dVector class that represents a mathematical vector but thousands of 2dVector objects in the game.
Note: The term instance is often used as a synonym for object. For example, the phrases “Make a new SpaceShip instance” and “Make a new SpaceShip object” mean the same thing. Creating a new object from a class is sometimes called instantiation. To build an object-oriented program, we:
- Create one or more classes.
- Make (i.e., instantiate) objects from those classes.
- Tell the objects what to do.
What the objects do determines the behavior of the program.
In addition to using the classes we create, a program can use any of the classes built into the Flash Player. For example, a program can use the built-in Sound class to create Sound objects. An individual Sound object represents and controls a single sound or a group of sounds. Its setVolume( ) method can raise or lower the volume of a sound. Its loadSound( ) method can retrieve and play an MP3 sound file. And its duration property can tell us the length of the loaded sound, in milliseconds. Together, the built-in classes and our custom classes form the basic building blocks of all OOP applications in Flash.
Class Syntax Let’s jump right into a tangible example. Earlier, I suggested that a space fighting game would have a SpaceShip class. The ActionScript that defines the class might looklike the source code shown in Example 2-1 (don’t worry if much of this code is new to you; we’ll study it in detail in the coming chapters).
Example 2-1. The SpaceShip class
class SpaceShip {
// This is a public property named speed.
public var speed:Number;
// This is a private property named damage.
private var damage:Number;
// This is a constructor function, which initializes
// each SpaceShip instance.
public function SpaceShip ( ) {
speed = 100;
damage = 0;
}
// This is a public method named fireMissile( ).
public function fireMissile ( ):Void {
// Code that fires a missile goes here.
}
// This is a public method named thrust( ).
public function thrust ( ):Void {
// Code that propels the ship goes here.
}
}
Notice how the SpaceShip class groups related aspects of the program neatly together (as do all classes). Variables (properties), such as speed and damage, related to spaceships are grouped with functions (methods) used to move a spaceship and fire its weapons. Other aspects of the program, such as keeping score and drawing the background graphics can be kept separate, in their own classes (not shown in this example).
 | If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!
Visit the O'Reilly Network http://www.oreillynet.com for more online content. |
Next: Object Creation and Usage >>
More Flash Articles
More By O'Reilly Media