Pave Posted September 22, 2018 Share Posted September 22, 2018 After certain game actions I want to remove some Sprites. Currently I am using: this.destroy(); But this doesn''t stop the execution of the currently running "preUpdate" function. So my game crashes, because the object doesn't exist anymore but the code continues to run till the end of the preUpdate and then stops. Is there a solution for this? I don't want to use "disableBody" or something like that, because I want the Garbage Collector to pick up the sprite. Link to comment Share on other sites More sharing options...
prob Posted September 22, 2018 Share Posted September 22, 2018 You'd need to post more complete code, because there is no context as to what this is referencing. Link to comment Share on other sites More sharing options...
Pave Posted September 22, 2018 Author Share Posted September 22, 2018 (edited) The Class: class Enemy extends Phaser.Physics.Arcade.Sprite { constructor (scene, x,y) { super(scene, x,y); } preUpdate (time, delta) { super.preUpdate(time, delta); } destroyEnemy(){ this.destroy(); } } class Enemy1 extends Enemy { constructor (scene, x, y) { super(scene, x, y); } preUpdate (time, delta) { super.preUpdate(time, delta); if(this.y > 300){ this.setVelocityY(this.body.velocity.y*-1); } } } In the scene: var config = { type: Phaser.AUTO, width: gameWidth, height: gameHeight, parent: 'game', resolution: window.devicePixelRatio || 1, physics: { default: 'arcade', arcade: { debug: true } }, scene: { preload: preload, create: create, update: update } }; var game = new Phaser.Game(config); function create () { // Add Groups Bullet.bullets = this.physics.add.group(); Enemy.enemies = this.physics.add.group(); // Check if bullet overlaps with enemy this.physics.add.overlap(Enemy.enemies, Bullet.bullets, enemyHit, null, this); } function enemyHit (enemy, bullet) { // Remove the enemy enemy.destroyEnemy(); // Remove the Bullet bullet.destroyBullet(); } After the collision the Enemy gets destroyed. But the game crashes when it tries to set the velocity of the non existing enemy. Edited September 23, 2018 by Pave Link to comment Share on other sites More sharing options...
Pave Posted September 24, 2018 Author Share Posted September 24, 2018 Am I doing something wrong? Link to comment Share on other sites More sharing options...
samme Posted September 24, 2018 Share Posted September 24, 2018 I don't know. It looks like destroy() removes its game object from the update list immediately, so I don't see how preUpdate would get called after that. Nonetheless, you can probably test this.active in preUpdate to avoid the error. Pave 1 Link to comment Share on other sites More sharing options...
Pave Posted September 25, 2018 Author Share Posted September 25, 2018 Yeah best solution I have found is using in the preUpdate: if(this.active == false){return;} Might not be the best but it works, could be a bit tideous as I have to copy it in ever sub-class. Link to comment Share on other sites More sharing options...
drogers18 Posted May 19, 2021 Share Posted May 19, 2021 I know this is 3 years old already but it saved me a LOT of greif. Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts