My tank shoots an enemy tank and only destroys the body without destroying the turret aswell. I want the tank body and the turret to destroy not just the enemy tank body.
Here is my code:
class Enemy extends Phaser.Physics.Arcade.Sprite {
constructor(scene, x, y, texture) {
super(scene, x, y, texture = 'player2', 'tank1')
scene.add.existing(this);
scene.physics.add.existing(this);
scene.events.on('update', this.update, this);
scene.physics.world.enableBody(this, 0);
this.setOrigin(0.5, 0.5);
this.setCollideWorldBounds(true);
this.body.setVelocity(100, 0);
this.body.bounce.setTo(1, 1);
this.health = 3;
//this.bullets = bullets;
this.fireRate = 1000;
this.nextFire = 0;
this.alive = true;
this.tank = this.scene.tank;
this.turret = new turret(scene);
this.shadow = new shadow(scene);
}
update(keys, time, delta) {
function enemyDestroy(Enemy, bulletHit)
{
if (bulletHit.active === true && Enemy.active === true)
{
Enemy.health = Enemy.health - 1;
console.log('enemy hp:', Enemy.health);
if (Enemy.health <= 0)
{
Enemy.setActive(false).setVisible(false);
}
bulletHit.setActive(false).setVisible(false);
}
}
this.turret.follow(this);
this.shadow.follow(this);
this.scene.physics.world.collide(this, player);
this.scene.physics.world.collide(this, playerBullets, enemyDestroy);
}
}
class turret extends Phaser.GameObjects.Sprite {
constructor(scene, x, y, texture) {
super(scene, x, y, texture = ‘player2’, ‘turret’)
scene.add.existing(this);
scene.events.on(‘update’, this.update, this);
this.setOrigin(0.3, 0.5);
}
follow(tank) {
this.setX(tank.x);
this.setY(tank.y);
}
update() {
this.rotation = Phaser.Math.Angle.Between(this.x, this.y, player.x, player.y);
}
}