Jump to content

How to destroy sprite when animation complete


bilboon
 Share

Recommended Posts

Hi all, 

 

I'm experiencing issue when trying to destroy a sprite at the end of an animation (phaser 2.2).

var sprite = game.add.sprite(0, 0, 'my_atlas'); // pseudo codesprite.animations.play('my_anim', 25, false);sprite.events.onAnimationComplete.addOnce(function (){    sprite.destroy();});

Error pop in Phaser.Animation.update :

this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);Uncaught TypeError: Cannot read property '20' of null

I think onAnimationComplete event is dispatched in the update method synchronously so sprite is destroyed and properties for proper update execution are missing.

 

How would you solve this use case ?

 

Thank you.

Link to comment
Share on other sites

I solved my pb creating a simili "garbage_collector" and calling it in state.update.

var gc = {    items: [],    add: function (item){        gc.items.push(item);    },    clear: function (){        for (var i=0, l=gc.items.length; i<l; i++){            gc.items[i].destroy();        }        gc.items = [];    }};function create () {    var sprite = awesome_sprite();    sprite.events.onAnimationComplete.addOnce(function (){        gc.add(sprite);    });}function update() {    gc.clear();}
Link to comment
Share on other sites

Another approach might be to use setTimeout with a time of 0. It won't really be 0, but it lets the event loop run once or twice and then call your function. I don't know if it'll work in your case, but it seems like less code so I thought I'd suggest it.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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