Jump to content

Problem in Restarting the game


abdul
 Share

Recommended Posts

Hi

 

I have a game restart function.

i.e

function 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.e

if(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.e

if(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

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

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

  • 2 years later...
 Share

  • Recently Browsing   0 members

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