Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

How to restart a Game.


aihimel
 Share

Recommended Posts

There is no set way to restart your game, its up to you to control how your game restarts and under what circumstances. However I think it can generally be broken down into two parts:

 

1. Detect that your game needs restarting. Maybe you want to restart the game when the user runs out of lives, or perhaps if he has shot a certain amount of enemies or if he clicks a restart button in your game. I would suggest putting some checks in your update function to see if the game need to be restarted, if they evaluate to true I would call a restart function.

 

2. Now you know that the gameplay needs restarting, how do you restart it? Well this is up to you, maybe you need to adjust the players position, or reset some score variables or add more enemies this is completely your decision to make dependent on your game - its your creation! 

 

This is the format that I would try and follow:

update function() { // Inside our update function so constantly checked   if(userClicksRestart() || userDead()){ // Check to see the game needs restarting      restart();  // Call the restart function   }}restart function() {   // These are just examples of what you might do   player.resetPosition(); // Reset the players position   score = 0;   // Reset the score to zero   addMoreEnemies();  // Add more enemies to your game}

I am not entirely sure what you are looking to do here, but this is just a brief overview of the logic that I might use to restart a game, I hope this helps. 

Link to comment
Share on other sites

I'm using a state to call the end screen that calls the main, play, state. Might be an ugly solution but works fine until I come up with something better.

  update: function() {    if (this.game.physics.collide (this.player, this.enemy)) {      this.player.kill();      game.state.start('Over');    }

This is my Game.Over state:

Game.Over = function(game) {};Game.Over.prototype = {  create: function() {    this.spacebar = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);    label = game.add.text(width / 2 , height / 2, 'Score: '+score+'\nGAME OVER\nPress SPACE to restart',{ font: '22px Lucida Console', fill: '#fff', align: 'center'});    label.anchor.setTo(0.5, 0.5);  },  update: function() {    score = 0;    if (this.spacebar.isDown)      game.state.start('Play');  }};

Hope that helps. :)

Link to comment
Share on other sites

  • 10 months later...
  • 2 years later...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...