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

Created on 27 Aug 2016  路  15Comments  路  Source: photonstorm/phaser

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.

Most helpful comment

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

All 15 comments

The trace suggests this.game is undefined, but that's weird; game should always be defined even when changing states.

I know right? Hopefully it's not my code that is messing it up. Anyone want to corroborate this?

Attempting to create a non-biased example shows that it is working fine. IDK what is going on with my code. I'll try to figure it out and update later.

Still haven't found the answer, will update/re-open when I do.

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

Wow nice!! Thanks man, good find! If you run this example the game will crash on state change and show this.game = null in the console.

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.

Thanks Rich! Since weapon is a default plugin, is there any way we could include this behavior by default for future users?

Hmm, sort of, it would require a new plugin level property along the lines of ignoreDestroy, so for cases where you want a single Weapon plugin shared across states, it didn't nuke it as soon as the state changed (which is how I tend to use it). Plugins never automatically destroy themselves on state change, so if Weapon did it, it'd be specific to that one alone. Definitely worth adding to the docs though.

Well can you think of any instance where a weapon would want to persist across states? It seems to almost be a first-class citizen along with Sprite's ilk. I'm not sure of any way to implement this tho, without reading the code more.

I can update weapon's docs in PR https://github.com/photonstorm/phaser/pull/2730.

Side question. My commit https://github.com/photonstorm/phaser/pull/2730/commits/f1a7976d83c92843c10dfaaaf6db42d976439640 in that PR got kinda borked somehow, Git thinks code changed that didn't actually change. Should I redo the PR or is that alright?

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.

I think what happened is the JS file was Beautified. But alright, sounds good!

Makes sense. And, please don't :)

Can I ask what you mean by this - how do you keep the bullets viable across state change?

I have a single instance, in a global (non-State) location, and just clear-down the bullets group on each state change...

Also, how do you manage having a ton of guns? Where each enemy has a gun, and the player could have a dual, tri, quad, etc shot weapon? I can imagine setting up a class specifically to manage Weapon pooling, and then just assigning multiple guns where a dual shot is needed?

I appreciate you taking my issue seriously and deprecating Phaser3 by the way! It's nice to have an impact on these big projects.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SKEPDIMI picture SKEPDIMI  路  4Comments

JarLowrey picture JarLowrey  路  4Comments

maikthomas picture maikthomas  路  4Comments

Colbydude picture Colbydude  路  4Comments

sercand picture sercand  路  3Comments