Game Development Reference
In-Depth Information
Chapter 29
Finishing the Tick Tick Game
In this chapter, you finish the Tick Tick game. First you add a timer such that the player has a
limited amount of time to complete each level. Then you add a few mountains and clouds to the
background to make the game visually more interesting. Finally, you progress through the levels by
adding two extra game states: the “game over” state, and the “level finished” state.
Adding a Timer
Let's first look at adding a timer to the game. You don't want the timer to take up too much screen
space, so you use a text version of it. Therefore, the TimerGameObject class inherits from the
Label class. You want to be able to pause the timer (for example, when the level is finished), so
you add a Boolean variable running that indicates whether the timer is running. You also store the
time remaining in a variable called _timeLeft . You override the reset method to initialize the timer
object. You want to give the player 30 seconds to finish each level. As a result, here is the complete
reset method:
TimerGameObject.prototype.reset = function () {
powerupjs.Label.prototype.reset.call(this);
this._timeLeft = 30;
this.running = true;
};
For convenience, you also add a property gameOver that indicates whether the timer has reached
zero. You use this property later to handle the event that the player doesn't finish the level in time:
Object.defineProperty(TimerGameObject.prototype, "gameOver",
{
get: function () {
return this._timeLeft <= 0;
}
});
371
 
Search WWH ::




Custom Search