Jump to content

Resetting Group Objects on Game Over


heisenthurg
 Share

Recommended Posts

When a players lives reach zero, I am trying to restart the game back to its initial state. I have read that there is no official restart function, so you have to essentially reset each aspect as necessary. I have successfully reset the score, lives, and player position, but can't seem to get the group of 'stars' that the player collects to reset.

 

I have a function for playerKill which occurs on collision with a certain layer. When the lives are less than 1, the player is killed, game over text is shown and, onTap calls a restart function, shown below. What can I add in here to remove the remaining stars? I have tried the revive(); function but that didn't seem to work. (I've also included some code where I create the group, in case this helps!)

//Create Stars sprite and animation    stars = game.add.group();    stars.enableBody = true;    this.map.createFromObjects('Stars', 25, 'stars', 0, true, false, stars);    stars.callAll('animations.add', 'animations', 'spin', [0, 1, 2, 3, 4, 5], 10, true);    stars.callAll('animations.play', 'animations', 'spin');//Restart function called 'onTap' on Game Over screen. The stars group needs to reset herefunction restart() {    goText.visible = false;    player.reset(150, 600);    lives = 3;    livesText.text = 'Lives: ' + lives;    score = 0;    scoreText.text = 'Score: ' + score; }

Thanks!

Link to comment
Share on other sites

I wasn't actually using states before, but I've now updated my game to have different states e.g. load, menu, play states etc. I wasn't going to do this, but now that I've created a game it definitely seems better to have it laid out in this way for expandability :) there's one issue I can't resolve now though, it is now having trouble calling one of my functions on click for the restart. Here are my functions that I am calling when the player reaches the end of the level, and the restart function I want to callback to on click:

    finishLevel: function() {        player.kill();        finalScore = score * lives;        finishText.text="YOU MADE IT!\nFinal Score: " + finalScore + "\nClick to try again";        finishText.visible = true;        game.input.onTap.addOnce(this.restart, this);    },    restart: function() {        game.state.start('play');    }

These functions both sit within the playState, and when I remove the onTap the finishText appears fine. I am seeing an error of "Phaser.Signal: listener is a required param of addOnce() and should be a Function." . I don't understand why this happens as restart is a function(?!). Any help would be appreciated!

 

Thank you!  :)

Link to comment
Share on other sites

OK so I used console.log(this); and it returns the finishLevel function, which doesn't contain the restart function. If I instead create a restart function within the finishLevel function, it works and the game restarts  :)

 

I would like this function to be used by several other functions (e.g. I have a gameOver function as well, which the player will need to restart from). Is there a way to write the restart function once and allow it to be used within other functions, so I don't have to write it out each time? I'm sure this has something to do with scope, but I'm a newbie to JS and am still getting my head around it!

 

Thanks for the help! 

Link to comment
Share on other sites

You can make the restart function as a separate function like 

function restart(){}

And it will be used globaly. Or you can save the restart function inside a global accessible variable I usually have an object that holds all globaly accessible items like this:

var registry = {restart: {},somevalue: 0};// and then on your create function do thisregistry.restart = this.restart;// then you can call restart from everywhere like this:registry.restart();
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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