I am trying to damage player or enemy when they collide.(enemy will be damaged when collided with bullet and player will be damaged when collided with monster)

this.game.physics.arcade.overlap(this.laser.bullets, this.zombies, this.hitEnemy, null, this);
this.game.physics.arcade.overlap(this.player, this.zombies, this.playerdmg, null, this);


player.damage(1); //oneshots



zombies.damage(1); // one shots
console.log(zombies.health) // this is -1 


Collision detection works.but i cant damage player or zombies. this is how i set health after creating player:


how do i set zombies health correctly ? because i create them with for loop like this:

this.zombies = this.game.add.group();
this.zombies.health = 5;
for (var i = 0; i < 30; i++)
        var zombie = this.zombies.create(this.game.world.randomX, this.game.world.randomY, 'zombie');
        let zombieHealthBar = new HealthBar(this.game, zombie, 25, 10, -20, 0, '#FF0000', '#00FF00');


zombies is a Group. You should set health on each individual zombie.

By the way, your function signature for hitEnemy is misleading. It is a callback that receives one bullet and one zombie, the pair that have overlapped.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.