Jump to content

[Resolved] How to properly deallocate GameObject animation


skornok
 Share

Recommended Posts

Hi,

 

in our game we're spawning enemies in some timely manner. The main game Scene implements a timer which takes care of spawning new instance of `Phaser.GameObjects.Sprite` every second. There are max. 13 enemies at once, each enemy is creating his own timer to follow a schedule when to attack etc.

Example of constructor of the enemy subclass

constructor ({ scene, hole }) {
		super(scene, params )

		this.timer = this.scene.time.addEvent({ 
			startAt: 1,
			delay: 3000,
			loop: true,
			callback: this.enemyTicker,
			callbackScope: this
		})

	}

For example this enemy type attacks every 3 seconds and his ticker implementation is following

alienTicker() {
    this.attack()
}

attack() {
    super.attack()

    this.play('simple_attack').anims.chain('simple_idle')
    this.play('simple_attack').once('animationcomplete-simple_attack', () => {
        this.scene.onAttack()
    })
}

On tapping the enemy I need to destroy the instance

tap() {
    super.tap()

    // Remove interaction
    this.removeInteractive()

    this.scene.onKill(this)

    this.play('simple_disappear').once('animationcomplete-simple_disappear', () => {
        this.finalize()
    })
}

finalize() {
    this.hole.onAlienDestroy()
    this.timer.remove()
    this.anims.remove()
    this.destroy()
}

In most cases it works but if I time my tap so that the enemy is about to attack and destroy him, I get the following exception

Uncaught TypeError: Cannot read property 'load' of null
    at Animation.load (Animation.js:420)
    at Animation._startAnimation (Animation.js:583)
    at Animation.play (Animation.js:530)
    at _default.play (Sprite.js:134)
    at _default.attack (SimpleAlien.js:78)
    at _default.alienTicker (SimpleAlien.js:118)
    at Clock.update (Clock.js:322)
    at EventEmitter.emit (index.js:203)
    at Systems.step (Systems.js:379)
    at SceneManager.update (SceneManager.js:563)

So clearly the problem is with the attack animation trying to be played but the instance doesn't exist anymore. Is there any other way how to stop animation manager from playing the animation than `this.anims.remove()`?

 

Thanks

 

EDIT:

The problem is in my sequence of the animations. I was cleaning the object when the dying animation ended but I should have stopped the timer on the start of the dying animation

		this.play('simple_explode').once('animationstart-simple_explode', () => {
			this.preclean()
		})

		this.play('simple_explode').once('animationcomplete-simple_explode', () => {
			this.postclean()
		})

 

Edited by skornok
resolved
Link to comment
Share on other sites

  • 11 months later...
 Share

  • Recently Browsing   0 members

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