
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Key Object-Oriented Programming Concepts
|
15
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
look like 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).
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 space-
ships 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 back-
ground graphics can be kept separate, in their own classes (not shown in this example).
Object Creation
Objects are created (instantiated) with the new operator, as in:
new ClassName()
where ClassName is the name of the class from which the object will be created.
For example, when we want to create a new SpaceShip object in our hypothetical
game, we use this code:
new SpaceShip( )
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, ...