Jump to content

animations object


giorg
 Share

Recommended Posts

Hi,

I'm correctly displaying a multiatlas animation this way:

this.anims.create({ key: 'open_1', frames: this.anims.generateFrameNames('open1'), repeat: 0 });
let op1 = ethis.add.sprite(490, 400).setScale(0.6).play('open_1');

then I need the animation to disappear once it's ended, so I'm doing:

op1.on('animationcomplete', ethis.openComplete, this);

but in the openComplete function I don't know how to access the op1 object to destroy it.

Any help?

Thanks a lot

Link to comment
Share on other sites

this.anims.create({ key: 'open_1', frames: this.anims.generateFrameNames('open1'), repeat: 0 });
this.op1 = this.add.sprite(490, 400).setScale(0.6).play('open_1');

op1.on('animationcomplete', ethis.openComplete, this);

op1 no longer block scoped and available as this.op1 in callback since you are passing the this context. Good enough?

Link to comment
Share on other sites

Either pass it as an argument to the openComplete method:

op1.on('animationcomplete', ()=>{ this.openComplete(op1); }, this);
this.openComplete = function(op1) {
    // op1 is available
    op1.destroy();
};

Or add the op1 variable as a property to the scene / class / object (whatever your scope is) as @cornstipated suggested:

this.op1 = this.add.sprite(490, 400).setScale(0.6).play('open_1');
this.op1.on('animationcomplete', this.openComplete, this);
// Scene context
SceneName.openComplete = function() {
    // this.op1 is available
    this.op1.destroy();
};

// Object prototyping
ObjectName.prototype.openComplete = function() {
    // this.op1 is available
    this.op1.destroy();
};

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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