Hi there, I have a class that holds my enemy tank and turret. Whenever my player tank destroys the enemy tank only the tank body seems to get destroyed and the turret stays. How do I make the turret disappear aswell?
Help would be much appreciated
class Enemy extends Phaser.Physics.Arcade.Sprite {
constructor(scene, x, y, key, texture) {
super(scene);
this.scene = scene;
this.x = x;
this.y = y;
this.enemy = this.scene.physics.add.sprite(x, y, 'enemy', 'tank1');
this.enemy.setOrigin(0.5, 0.5);
this.enemy.body.immovable = false;
this.enemy.body.collideWorldBounds = true;
this.enemy.body.bounce.setTo(1, 1);
this.enemy.setVelocity(100, 0);
this.enemy.health = 3;
this.turret = this.scene.physics.add.sprite(x, y, 'enemy', 'turret');
this.turret.setOrigin(0.3, 0.5);
this.scene.add.existing(this);
this.scene.physics.add.existing(this);
this.scene.events.on('update', this.update, this);
}
update() {
this.turret.x = this.enemy.x;
this.turret.y = this.enemy.y;
function enemyHitCallback(enemyHit, bulletHit)
{
// Reduce health of enemy
if (bulletHit.active === true && enemyHit.active === true)
{
enemyHit.health = enemyHit.health - 1;
console.log("Enemy hp: ", enemyHit.health);
// Kill enemy if health <= 0
if (enemyHit.health <= 0)
{
enemyHit.setActive(false).setVisible(false);
}
// Destroy bullet
bulletHit.setActive(false).setVisible(false);
}
}
this.scene.physics.world.collide(this.enemy, playerBullets, enemyHitCallback);
}
}