aihimel Posted February 16, 2014 Share Posted February 16, 2014 Hello Everyone,I am trying to develop a breakout type game. But can't restart the game. Is there any easy way to do that? Thanks in advance. Link to comment Share on other sites More sharing options...
JackBid Posted February 16, 2014 Share Posted February 16, 2014 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. aihimel 1 Link to comment Share on other sites More sharing options...
ZRT Posted February 16, 2014 Share Posted February 16, 2014 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. aihimel, Teemu-Tor and Heppell08 3 Link to comment Share on other sites More sharing options...
aihimel Posted February 17, 2014 Author Share Posted February 17, 2014 Thanks JackBid and ZRT for answering. I think that will solve my problem. Link to comment Share on other sites More sharing options...
metalNumb Posted January 4, 2015 Share Posted January 4, 2015 This seemed to do the trick for me.this.game.state.start("the_state_name"); Link to comment Share on other sites More sharing options...
samme Posted September 16, 2017 Share Posted September 16, 2017 this.game.state.restart() if the current state is the reentry point. Link to comment Share on other sites More sharing options...
Recommended Posts