Game Development Reference
If you take a car as an example, each component of a car (such as the steering wheel, the engine, and
the ignition switch) performs a single specific function. If you were building a car with ActionScript,
the steering wheel would be one class, the engine another class, and the ignition switch yet another
class. Each of those classes is a perfectly useful, self-contained unit in its own right, but also works with
the others to create the single bigger and better unit called a “car.”
There is a subtle but extremely important difference between our car example and an
actual class that I'll touch on briefly (it will become much clearer in the chapters that
follow). A class is also a blueprint for other objects that share its same properties or
behaviors. If that sounds confusing, think of the wheel of a car. Most cars have four
wheels that look exactly the same and do exactly the same thing: they roll. In fact, the
only difference between them is their names: “left front wheel,” “right front wheel,”
“left back wheel,” and “right back wheel.” Other than that, they're completely identi-
cal. So if you're designing a car, why go to the all the trouble of designing four wheels
from scratch? Why not just design one wheel and make four copies of it?
That's exactly the convenience that classes provide. A class is like a master template or
a cookie cutter that you can use to make as many copies as you want. That is, in fact,
exactly what the word class literally refers to—a master category of things that share
the same properties.
For example, a car designer might create one wheel called Sdaah. The designer could
then make new copies (or instances ) of that parent Sdaah class called habpBnkjpSdaah,
necdpBnkjpSdaah, habp>]_gSdaah, and necdp>]_gSdaah In all four cases, the wheels'
behaviors and properties are exactly the same as their parent Sdaah class; only their
names are different.
You'll encounter the word instance a lot in this topic. If a class is like a printing press,
an instance is the printed paper the press produces. Instances contain exactly the
same properties as the original, but are individual objects in their own right. When an
instance of a class is made, it's said to be instantiated .
This example is a slight oversimplification, but the important thing to remember is
that when you create a class, you're also creating a master template that you can
use to make as many individual instances of that class as you need, without having to
rebuild from scratch each time.
Any ActionScript program you write must have at least one class, but most of the programs and games
you'll be building in this topic will have many. Take a quick look at the new code:
This code is called a class definition . Just like the package, a class definition is a type of block state-
ment. You can tell this by the class's own pair of curly braces, hanging there in space. (Those braces
are empty at the moment, so the class can't do anything yet. That will change very soon.)