Game Development Reference
In-Depth Information
This is where the constructor method comes in. It makes things happen. Specifically, it throws the
class into action as soon as the class is called upon by being instantiated, and any programming code
it contains is run instantly. If the class definition alone is just an empty shell, the constructor method is
like its heart and lungs. Fear not; you can come back into the room now. The constructor method is on
your side!
4. Now create the constructor method for the I]ej class. Add the code in bold to the code you
already wrote:
As you can see, the constructor method is simply a block statement that looks an awful lot like the
class definition. In fact, it has the same name: I]ej. This is no accident: all classes need to have con-
structor methods that are named exactly the same as the class name.
The other thing you'll recognize is the keyword lq^he_. As with the class definition, using the word
lq^he_ tells Flash that the constructor method is freely available to be used by different classes from
different packages. (A strange quirk of the AS3.0 language, however, is that constructor functions can
only ever be lq^he_.)
One new thing is the bqj_pekj keyword, which tells Flash that you're creating a function definition .
Function definitions are simply block statements that perform actions. They do all the heavy lifting
for your program. You can think of function definitions as dutiful servants who snap to attention with
a prearranged set of chores as soon as you call on them. The constructor method, which will always
be the first function definition you write when you create a class, has the special job of running any
actions it contains immediately—as soon as the class is called upon and before any other methods
you might be using are put to work. The constructor method is a bit like the head servant who's up
at the crack of dawn, gets all the other servants out of bed, and greets you with a fresh pot of tea and
the morning paper before you've even found your slippers.
The last thing you should notice about the constructor method is the set of parentheses after the
method name:
Those empty parentheses allow you to provide the method with extra information, known as param-
eters , if the method needs it. You'll look at method parameters in detail fairly soon, but for now you
just need to know that you must provide a set of parentheses when creating a function definition,
even if those parentheses are empty.
Search Nedrilad ::

Custom Search