Tinto

Assign a life for each enemy in a group

Recommended Posts

Hi, I don't know how to assign the life variable individually to each zombie, avoiding that when one is hit, life is taken away from everyone.

Same thing for the infectStatus which affects all zombies and not the individual.

Thanks a lot!

 

var zombies; 
var zombieLife = 3


this.create = () => {

zombies = game.add.group();
zombies.enableBody = true;
zombies.physicsBodyType = Phaser.Physics.ARCADE;

var zombie0 = game.add.sprite(500, 1400, 'zombie');
var zombie1 = game.add.sprite(600, 1400, 'zombie');

zombies.add(zombie0);
zombies.add(zombie1);

zombies.setAll('anchor.x', 0.5);
zombies.setAll('anchor.y', 0.5);
zombies.setAll('body.gravity.y', 2000);
zombies.setAll('body.velocity.x', 100);
zombies.setAll('body.collideWorldBounds', true);

}

this.update = () => {
game.physics.arcade.overlap(player, zombies, touchZombie);
}


  function touchZombie(w, p) {
    if (player.body.touching.right && p.body.touching.left && !infectStatus) {
      if (!atkStatus) {
        infectStatus = true;
        life = life - 30;
        game.time.events.add(2000, function() {
          infectStatus = false;
        })
      } else if (atkStatus) {
        return zombieKill(p, w)
      }
    } else if (player.body.touching.left && p.body.touching.right && !infectStatus) {
      if (!atkStatus) {
        infectStatus = true;
        life = life - 30;
        game.time.events.add(2000, function() {
          infectStatus = false;
        })
      } else if (atkStatus) {
        return zombieKill(p, w)
      }
    } else if (player.body.touching && p.body.touching && infectStatus) {
      if (!atkStatus) {
        player.tint = 0xFF0000;
        game.time.events.add(2000, function() {
          player.tint = 0xffffff;
        })
      } else if (atkStatus) {
        return zombieKill(p, w)
      }
    }
  }


function zombieKill(w, b) {
    var zombieLife = 3
    if (zombieLife > 0 && !hitZombie) {
      zombieLife--
      hitZombie = true
      w.tint = 0xFF0000;
      game.time.events.add(500, function() {
        hitZombie = false;
        w.tint = 0xffffff;
      })
    } else if (zombieLife <= 0) {
      w.body.velocity.x = 0;
      w.animations.play('killed');
      game.time.events.add(500, function() {
        game.add.tween(w).to({
          alpha: 0
        }, 1000, Phaser.Easing.Linear.None, true, 1400, 0, false);
        game.time.events.add(2400, function() {
          w.kill();
        })
      })
    }
  }

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.