phaser: Switching states while Phaser.autofire is on causes a crash

This Issue is about (delete as applicable)

  • A bug in the API

Example

      var weapon = this.game.add.weapon(30);
      weapon.autofire = true;
      this.game.state.start('GameOver' ); //ERROR!

Trace

//error occurs here in Phaser.Group.add
this.game.physics.enable(child, this.physicsBodyType);

selection_028

My hypothesized source:

Phaser.Weapon.prototype.update = function () {

    //...stuff...

    if (this.autofire)
    {
        this.fire();
    }

};

I suspect the update function continues to run even after the state has changed. Autofire is set to true, fire() is called, eventually down the stack something breaks.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 15 (11 by maintainers)

Most upvoted comments

I think you also need weapon.autoExpandBulletsGroup: true to see this error.

You are responsible for tidying-up plugins, and any of the other countless things that you could have done in a game state, during the states shutdown method. It’s there for last-minute house-keeping, object nulling, reference clearing, etc. And that’s definitely what this falls in to.

Calling destroy on any Weapon instance you’ve created will be enough. If it isn’t, then feel free to re-open as a bug that needs fixing. But looking quickly at the destroy code, I’m pretty sure it will work fine.

Yeah I use the Weapon plugin in a shmup where I have a single instance, in a global (non-State) location, and just clear-down the bullets group on each state change, but keep everything else the same (bullet count, last shot timer, etc). This will be even more important when we add parallel state / non-destructive states. For now though, let’s update the docs for autoFire, and maybe in the class description too. That’s an easy to merge change.