Game Development Reference
In-Depth Information
Defining the constructor and init function for SuperClick.as
The constructor function for our game is an empty function that does not need to do any
processing.
public function SuperClick() {
}
In later games, we might call other functions such as an initialization function (sometimes called
init
) that will set up variables and elements that the game needs before the framework calls the
newGame
function. Super Click is a very simple game that does not need to do this.
override public function newGame():void {
trace("new game");
level = 0;
score = 0;
gameOver = false;
dispatchEvent(new CustomEventScoreBoardUpdate
(CustomEventScoreBoardUpdate.UPDATE_TEXT,
Main.SCORE_BOARD_SCORE,"0"));
dispatchEvent(new CustomEventScoreBoardUpdate
(CustomEventScoreBoardUpdate.UPDATE_TEXT,
Main.SCORE_BOARD_CLICKED,"0/0"));
dispatchEvent(new CustomEventScoreBoardUpdate
(CustomEventScoreBoardUpdate.UPDATE_TEXT,
Main.SCORE_BOARD_PERCENT_NEEDED,"0%"));
dispatchEvent(new CustomEventScoreBoardUpdate
(CustomEventScoreBoardUpdate.UPDATE_TEXT,
Main.SCORE_BOARD_PERCENT_ACHIEVED,"0%"));
}
The first thing that might catch your eye in this code is the
override
directive in front of the
function name. We must do this because the
Game.as
class that we extend to create
SuperClick.as
already has a
newGame
function stub created.
In this code, we reset the
score
and
level
variables to
0
and set the
gameOver
Boolean to
false
.
These are the basic variables that almost all games will have to initialize in their
newGame
functions. Some games may not have a score, and some may not have levels, but almost all
games will have some sort of game over state.
After the variable initialization, we dispatch the custom
Event
instances to the listeners in
Main
to
update the
scoreBoard
and reset the values to the game start values. The
key
passed is a
constant we have predefined in the
Main.as
class. For example, to update the score value on the
scoreBoard
, we pass in
Main.SCORE_BOARD_SCORE
as the key and
0
for the value.
If you recall from Chapter 2, the
value
is the same as the
value
in the
ScoreBoard
class when calling
the
ScoreBoard.update
function. The
SuperClick.as
class does not call these directly, but
dispatches an event that
Main
listens for (the
scoreBoard
is a child of the
Main
class, not
Game
or its
subclasses like
SuperClick
). This allows the
Main.as
to also update and maintain the
scoreBoard
.