Game Development Reference
In-Depth Information
ejep$%7
y
bqj_pekjejep$%6rke`
w
pn]_a$Dahhk(bnkipdaejepiapdk`%7
y
y
y
2. Save the I]ej*]o file and test the program. If everything is working properly, you should see
the words Hello, from the init method! displayed in the Output panel.
What's going on here? Let's have a closer look at the constructor method:
lq^he_bqj_pekjI]ej$%
w
ejep$%7
y
You might recall from Chapter 1 that the directives inside the constructor method are automatically
run when the class is instantiated. You have only one directive in the constructor method:
ejep$%7
What is this? It's a method call . Method calls trigger a method's function definition to run its direc-
tives. The method being “called” here is the ejep method. This means that as soon as the constructor
method runs, it immediately tells the ejep method to perform whatever tasks you assigned to it. The
ejep method's function definition is declared just below the I]ej constructor method:
bqj_pekjejep$%6rke`
w
pn]_a$Dahhk(bnkipdaejepiapdk`%7
y
ejep is programmer's shorthand for initialize . (You could have easily called the method ejepe]heva or
Ejepe]hevaC]ia if you had wanted to, but using the short form is another one of those “old program-
mer's conventions” that won't harm you to get used to because it's widely used.) The ejep function
definition is empty at the moment except for the pn]_a directive, but all the directives that will be
used to initialize the program will eventually be inside it. In the previous examples, all the directives
that initialized the program were inside the constructor method. What's the point of creating another
method to do this job now?
The biggest advantage is that whenever you need to reinitialize your program, all you have to do is call
the ejep method again from anywhere in your program. Let's pretend that you're playing the number
guessing game and hopelessly lose the first time you play. If you want to play again, it would be nice
if the game would reset the number of guesses you have left to the maximum and choose a new
mystery number for you to guess. If you have all the game's default settings conveniently tucked away
inside an ejep method, you just need to call this method whenever you want to reset the game and
play again. (In fact, you'll do this later in the chapter.)
 
Search Nedrilad ::




Custom Search