Sign in to follow this  
aihimel

How to restart a Game.

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. 

Share this post


Link to post
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. :)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.