abdul Posted July 12, 2014 Share Posted July 12, 2014 Hi I have a game restart function.i.efunction restart () { game.state.restart();}I am calling it two times, one time when player all lives kill, then I am calling it from update function, and it is working well. i.eif(player.position.y>game.world.height){ live = lives.getFirstAlive(); if (live) live.kill(); player.reset(120, game.world.height - 70); if (lives.countLiving() < 1){ player.kill(); stateText.text=" GAME OVER \n Click to restart"; stateText.visible = true; //the "click to restart" handler game.input.onTap.addOnce(restart,this); } }second time I am calling it when player wins the game, and wish to restart it, but this it doesn't restart the game ... i.eif(diamondScore>=2 && starScore>=2) { player.kill(); stateText.text=" Congrajulations\nYou Won \n Click to restart"; stateText.visible = true; game.input.onTap.addOnce(restart,this); }anyone has any idea regards this,,, Link to comment Share on other sites More sharing options...
lewster32 Posted July 12, 2014 Share Posted July 12, 2014 This looks like a scope issue. 'this' may not be what you expect it to be depending on if things are inside functions etc. To be sure, rather than do it your way, try it this way:game.input.onTap.addOnce(function() { game.state.restart();}, this);Because 'game' is definitely accessible when you add the handler, it'll also definitely be accessible within this context. Then you don't have to worry about where your 'restart' function is in relation to the current context. Link to comment Share on other sites More sharing options...
abdul Posted July 12, 2014 Author Share Posted July 12, 2014 game.input.onTap.addOnce(function() { game.state.restart();}, this);It is not working..same problem..it resets all things, but the text "Congrajulations You Won Click to restart" remains there and player doesn't come. Link to comment Share on other sites More sharing options...
lewster32 Posted July 12, 2014 Share Posted July 12, 2014 You may need to add a shutdown function to your state and clean up any stuff that's left over. I'm afraid I don't know a lot about states in the case of using them to manage scenes, so I can't assist very much in this topic. Link to comment Share on other sites More sharing options...
ITpaolo Posted October 12, 2016 Share Posted October 12, 2016 On 12.7.2014 at 8:47 PM, lewster32 said: game.input.onTap.addOnce(function() { game.state.restart();}, this); Worken for me like a charm, thanks mate :-) Link to comment Share on other sites More sharing options...
Recommended Posts