Game Development Reference
In-Depth Information
public function systemNewGame():void {
addChild(game);
game.addEventListener(CustomEventScoreBoardUpdate.UPDATE_TEXT,
scoreBoardUpdateListener, false, 0, true);
game.addEventListener(CustomEventLevelScreenUpdate.UPDATE_TEXT,
levelScreenUpdateListener, false, 0, true);
game.addEventListener(CustomEventSound.PLAY_SOUND, soundEventListener,
false, 0, true);
game.addEventListener(Game.GAME_OVER, gameOverListener, false, 0, true);
game.addEventListener(Game.NEW_LEVEL, newLevelListener, false, 0, true);
game.newGame();
switchSystemState(FrameWorkStates.STATE_SYSTEM_NEW_LEVEL);
}
Note that many developers like to use naming convention
onXxxxxEvent()
to name callback
functions for events. This is a fine way to name your events, and if you feel like renaming the
events in this topic to match that style you are more than welcome to do so.
We also need to remove the
EventListener
when the game is over. To do that, we add another
line of code to the
gameOverListener()
function:
public function gameOverListener(e:Event):void {
switchSystemState(FrameWorkStates.STATE_SYSTEM_GAME_OVER);
game.removeEventListener(CustomEventScoreBoardUpdate.UPDATE_TEXT,
scoreBoardUpdateListener);
game.removeEventListener(CustomEventLevelScreenUpdate.UPDATE_TEXT,
levelScreenUpdateListener);
game.removeEventListener(CustomEventSound.PLAY_SOUND, soundEventListener);
game.removeEventListener(Game.GAME_OVER, gameOverListener);
game.removeEventListener(Game.NEW_LEVEL, newLevelListener);
}
The
soundEventListener()
function is brand new in the
GameFrameWork
class. When the
CustomEventSound
event is dispatched, we will set all the properties of the
CustomEventSound
object necessary to play a sound. As a review, those properties follow:
type
: This
String
defines the type of event, either
CustomSoundevent. PLAY_SOUND
or
CustomSoundevent.STOP_SOUND
name
: This
String
determines the sound to play. Values are static
const
s defined in
Main.as
:
Main.SOUND_XXX
isSoundTrack
: This
Boolean
indicated whether to consider this sound a soundtrack when
playing.
■
Note
We will talk about playing sound tracks in Chapter 8.
loops
: This
int
sets the number of times to loop the sound when playing.