Game Development Reference
In-Depth Information
heartsNeeded = 0;
heartsCollected = 0;
setupWorld();
countDownTimer.seconds = levelTimerStartSeconds;
countDownTimer.min = 0;
goalReached = false;
dispatchEvent(new CustomEventLevelScreenUpdate(CustomEventLevelScreenUpdate.
UPDATE_TEXT, String(level)));
dispatchEvent(new CustomEventHeartsNeeded(CustomEventHeartsNeeded.
HEARTS_NEEDED, String(heartsNeeded)));
restartPlayer();
}
A detailed description of the main points of this function follows. The
stage.focus = this;
code
ensures that the game will have keyboard focus and accept events without the user having to
click the stage.
1.
First, we check for the level rollover. If the previous level was the final level for the
game, we reset to the first level.
2.
We set our variables for collecting the hearts—
heartsTotal
,
heartsCollected
, and
heartsNeeded
—back to
0
.
heartsTotal
represents a count of all the hearts in the level.
heartsNeeded
is
heartsTotal
multiplied by the
levelPrecentNeeded
variable.
heartsCollected
will count up the hearts the player collects on the level.
3.
Next, we call the
setupWorld
function that will read in and format the tile data. We will
cover this one next.
4.
We reset the
countDownTimer
by setting the start seconds to
levelTimerStartSeconds
;
the minimum for the counter is 0.
5.
We set
goalReached
to be
false
. This will be set to true when
heartsCollected
is
greater than or equal to
heartsNeeded
and the goal sprite has been reached.
6.
We dispatch a custom event to reset the hearts needed on the scoreboard.
7.
We call the restart player function.
The setUpWorld function
The
setupWorld
function that follows is a combination of the
readBackGroundData
and the
readSpriteData
functions from the No Tanks! game in Chapters 6 and 7. It reads in the level and
parses it to create the current level.
private function setupWorld():void {
world = [];
var tileNum:int;
var numberToPush:int;
levelData = levels[level];