Game Development Reference
In-Depth Information
keyListenersInit = true;
}
countDownTimer.addEventListener(BasicFrameTimer.TIME_IS_UP, timesUpListener,
false, 0, true);
}
Here is a detailed description of the main points of this function:
1.
First, we set the internal state machine to
SYSTEM_STATE_GAMEPLAY
with a call to the
switchSystemState
function.
2.
Next, we call the
initTileSheetData
function, which we will cover in the next section.
3.
We initialize our
player
CarBlitSprite
instance by passing in a reference to the
tileSheet
; the
playerFrameList
that will be set in the
initTileSheetData
function and
the start frame from the car animation loop.
4.
After that, we add the player to the display list and reset the
score
,
level
, and
gameOver
variables to their default new games values.
5.
We dispatch three custom event instances to reset our
ScoreBoard
fields to their
default new games values.
6.
We add the key listeners to the game only before the first game. Because our
Game
instance's constructor is called in
Main
before the stage is ready, we cannot initialize
these in the
init
function. We do it here because we are certain that the stage is
initialized, but we only need to do it for the first game.
7.
We add a listener to the
DriveSheSaid
class to listen for the
TIME_IS_UP
event in the
CountDownTimer
class instance.
The initTileSheetData function
This is the complete code listing for the
initTileSheetData
function:
private function initTileSheetData():void {
playerFrameList = [];
tileSheetData = [];
var numberToPush:int = 99;
var tileXML:XML = TilesheetDataXML.XMLData;
var numTiles:int = tileXML.tile.length();
for (var tileNum:int = 0; tileNum < numTiles; tileNum++) {
if (String(tileXML.tile[tileNum].@type) == "walkable") {
numberToPush = TILE_MOVE;
}else if (String(tileXML.tile[tileNum].@type) == "nonwalkable") {
numberToPush = TILE_WALL;
}else if (tileXML.tile[tileNum].@type == "sprite") {
switch(String(tileXML.tile[tileNum].@name)) {
case "player":
numberToPush = SPRITE_PLAYER;
playerFrameList.push(tileNum);