I have a class for my enemy in my game which creates the sprite by adding the sprite to an enemies group and in the update function of the class, I check when the health of the enemy is small enough the enemy dies. However, the code doesn’t cause the enemy to disappear from the game.
Here is my enemy class
class Enemy {
constructor(scene,x,y,name,health,enemies) {
this.scene = scene;
this.x = x;
this.y = y;
this.name = name;
this.cursors = cursors;
this.game = scene.game
this.health = health;
this.enemy_sprite = enemies.create(x,y,name)
this.enemy_sprite.setScale(0.27);
console.log(this.health);
this.enemy_sprite.setDrag(100, 0);
this.scene.physics.add.collider(this.enemy_sprite,projectiles,(enemy) => {
this.health = this.health - 10;
console.log(this.health)
})
this.enemy_sprite.setCollideWorldBounds(true);
}
update(){
this.x = this.enemy_sprite.x;
this.y = this.enemy_sprite.y;
if (this.health <= 0){
enemies.destroy(this.enemy_sprite)
}
}
}