Game Development Reference
In-Depth Information
A class definition does three main things:
It creates the class. The keyword _h]oo tells Flash that you are creating a class. Simple
enough!
It gives the class some rules to follow. This should be obvious: how many of you have been in
a class without rules? This code tells Flash that the class you're creating is lq^he_. That means
that the information it contains can be shared by any other classes that are part of the same
package or part of any other packages your program might be using. The information the class
contains is freely available to all. (Although most of this topic uses public classes, sometimes
you'll want to prevent classes from sharing information, much like a school football team
would not want to share its strategy with a competing school. In a case like that, you would
define a class as ejpanj]h. Internal classes share their information only with other classes that
reside in the same package.)
It gives the class an identifier, a name, which can be any name you choose. In this case, the
class is called I]ej. The M is capitalized because, by convention, all class names begin with
a capital letter.
You're not experiencing déjà vu! You have seen the word Main before. It's the name of
the AS file that you're working on: I]ej*]o. This is no coincidence. When you create
a class, it has to be saved in a file that has an identical name to the class name. For
every new class you create, you must create a new AS file that shares the same class
name.
The choice of the name Main is an old programmer's convention for the first file that
gets the program up and running. Like the ignition switch on a car, it fires up the pro-
gram and kick starts any other files you might be using. And when eventually you're
working on big game-design projects with hundreds of files, you'll always remember
that your main file is called I]ej. You'll see how all this works in later in the topic, but
for the first few chapters you'll be doing all the programming exclusively inside the
I]ej class.
The class definition's poor little curly braces are still empty. Let's put them to use!
Using the constructor method
The constructor method . That sounds daunting, doesn't it? In fact, it has the ring of science fiction-like
doomsday peril about it. Like a kind of interdimensional quantum death ray used by a race of robotic
drones to crush the spaceships of rival empires into the tin can-sized condiments they sell in their
cafeteria vending machines. But before you put this topic down and tiptoe quietly from the room,
take a small step back and look at what you've done so far.
First, you created a package. But the package was nothing more than an empty container for the class.
So next you created a class. But that was nothing more than an empty container. Lifeless! What to do?
It seems so hopeless!
 
Search Nedrilad ::




Custom Search