Until now, we learned how to detect the click / touch on a button, but this time we don’t have buttons on the stage. We will need to trigger a generic click/touch. In the same way Phaser handles generic input on buttons, it also handles generic input events.
Look at these new changes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
gameOver.prototype = { create: function() { var style = { font: "32px Monospace", fill: "#00ff00", align: "center" } var text = game.add.text( game.width / 2, game.height / 2, "Game Over\n\nYour score: " + score + "\n\nTap to restart", style ); text.anchor.set(0.5); game.input.onDown.add(this.restartGame, this); }, restartGame: function() { tilesArray.length = 0; selectedArray.length = 0; game.state.start("TitleScreen"); } } |
Apart from restartGame function which simply restarts the game by calling TitleScreen state after clearing out the arrays, I want you to focus on this line:
1 |
game.input.onDown.add(this.restartGame, this); |
Wherever you click or touch the screen, restartGame method will be called.
input.onDown(callback, callbackContext) is fired each time a pointer is pressed down. It runs callback function in callbackContext context).
And finally the game is complete. Anyway, what happens if you removed all tiles before time runs out? You will find yourself looking at a black screen while the timer ticks away without you being able to do anything.
That’s why we are going to add another feature. The last one, in this game.
Download Source
File: restarting-the-game.zip