przemoo83 Posted March 2, 2015 Share Posted March 2, 2015 Hi I have a problem when I try to restart game when player dies. I do it like this if(playerHealth <= 0) { this.game.state.restart(); } It works correctly because it restarts the stage hovever it keeps restarting itself over and over again as if the playerHealth was 0 all the time. This is strange because in this stage I declared global variable playerHealth = 100; so I don't think that is the problem. Any ideas? Link to comment Share on other sites More sharing options...
Kristonitas Posted March 3, 2015 Share Posted March 3, 2015 Hi, from my experience stateManager class will reset all of the sprites and stuff (because you probably placed them in the create method, not the constructor)MyState = function(){ playerHealth = 100 //(default starting health)}MyState.prototype.create = function(){ playerVelocity = 50 //(initial player velocity)}To get the desired behavior you should initialize your variables in create function, not your object function (the first function). That would indicate that phaser doesn't remake the whole state object. Having that in mind, you might want to clear every single variable at some point when resetting the state. You should look into the source code of phaser to find your answers, some things are easier to understand than you might think. Link to comment Share on other sites More sharing options...
blizzardmuffin Posted March 4, 2015 Share Posted March 4, 2015 Do you ever reset the player's health to 100, or whatever the max is? Otherwise the if statement will always be true. Link to comment Share on other sites More sharing options...
B3nac Posted March 4, 2015 Share Posted March 4, 2015 You could reset the player's health then call the restart state in that function. One way or the other the player's health would have to be reloaded or you will be stuck in a loop. Link to comment Share on other sites More sharing options...
Recommended Posts