Cannot called super() on extended Image

Hi. I am extending the Image class to create a Character. I have a lot more code in there that is all working as expected. I have a destroy overloaded function as I want it to display a little ‘fade out’ animation before then destroying. When the animation is complete I want to call the destroy super. However this isn’t working, I get a breaking error that super isn’t available. What next?

export class Character extends Phaser.GameObjects.Image {
	destroy() {
	    let self = this;
	    //Animate
	    this.scene.tweens.add({
	      targets: self,
	      scale: self.scale * 2,
	      alpha: 0,
	      ease: 'Power2',
	      duration: 250,
	      onComplete: function () {
		      self.super();
	      }
	    })
	  }
}

Hi !

Why call super here ? Just this.destroy should work great

this.destroy would cause an infinite loop in this case, since destroy is being overridden. You should use an arrow function and call super.destroy():

onComplete: () => {
    super.destroy();
}

I’m not convinced that this is a good solution, however. When destroy is called, you’d usually expect the Game Object to be destroyed instantly. If the scene is shutting down, the systems (such as scene.tweens) will shut down, too, so the code will not work. I’d recommend putting this code in a separate method.

1 Like

If you do override destroy you should forward its argument (fromScene).

1 Like