One key game mechanic of video games is to add challenge to a game by making the player complete something within a certain time.
In this section we will do this with a simple count down timer.
Our count down timer will be visible in the top left. When it counts down to zero then it will trigger the starting of the game over state.
This mechanic using the following techniques which are covered in more detail in other parts of this book;
this.timeLimit = 5;
this.timeText = game.add.text(10, 10, "0:00");
this.timeText.fill = "#000000";
this.timer = game.time.events.loop(1000, this.tick, this);
playState.tick = function () {
this.timeLimit--;
var minutes = Math.floor(this.timeLimit / 60);
var seconds = this.timeLimit - (minutes * 60);
var timeString = this.addZeros(minutes) + ":" + this.addZeros(seconds);
this.timeText.text = timeString;
if (this.timeLimit === 0) {
game.state.start("gameover");
}
};
When it comes to the gameover state there isn't too much new here. It is worth mentioning that here we have created a separate function to restart the game which gets called via a timer event after two seconds (2000 miliseconds.
var gameoverState = {};
gameoverState.create = function () {
this.gameoverText = game.add.text(10, 10, "Game Over");
this.gameoverText.fill = "#ffffff";
game.time.events.add(2000, this.restart, this);
};
gameoverState.restart = function () {
game.state.start("play");
};
There has been error in communication with Booktype server. Not sure right now where is the problem.
You should refresh this page.