Jump to content

Properly shutting down upon state change?


Mike018
 Share

Recommended Posts

I'm just now starting to clear variables, tweens, and sprites on state change and have some questions since I never see this stuff in phaser tuts.

  • Is the code below properly stopping, removing and clearing variables of the text, audio, sprites, and tweens?
  • If a tween does not have an onComplete in the code, do I have to do .onComplete.removeAll during the shutdown or is stopping and setting the variable to null sufficient? 
  • When a sprite has its input enabled and events attached to it, do I have to manually clear that or is it removed once the sprite is killed? What about when it's destroyed?
  • Is there a nicer way to clear everything instead of having so many if statements as well as having to make every variable reachable outside the method scope, maybe putting every sprite in a group and doing a forEach to remove it?

If you have an example of your shutdown methods, I'd like to see it!

shutdown() {

    //DESTROY BITMAP TEXT
    this.text = null;

    //DESTROY AUDIO
    if (this.audio) {
      this.audio.stop();
      this.audio = null;
    }

    //KILL SPRITES
    if (this.startButton) {
      this.startButton.kill();
      this.startButton = null;
    }

    //DESTROY TWEENS
    if (this.tween) {
      this.tween.onComplete.removeAll();
      this.tween.stop();
      this.tween = null;
    }

  }

 

Link to comment
Share on other sites

To the codes! https://github.com/photonstorm/phaser/blob/master/src/core/StateManager.js#L384

That's everything Phaser does for you when shutting down a state. All the tweens are killed, for instance. Depending on how you transition to a new state the world is also cleared, which means you *probably* don't need to kill individual sprites. At least, I never do.

Failing that, if "this.startButton" has a chance of being null in shutdown then, no, you must conditionally destroy it. You could shorten that to "this.startButton && this.startButton.destroy();" if you're going for a brevity thing.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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