Time for action – interacting with zombies

  1. In the Update() method of the LevelManager class, replace the foreach loop that updates the enemies list entries with the following loop:
    for (int x = enemies.Count - 1; x >= 0; x--)
    {
        enemies[x].Update(gameTime);
        if (!enemies[x].Dead)
        {
            if (player.CollisionRectangle.Intersects(
                enemies[x].CollisionRectangle))
            {
                if (player.WorldCenter.Y < enemies[x].WorldLocation.Y)
                {
                    player.Jump();
                    player.Score += 5;
                    enemies[x].PlayAnimation("die");
                    enemies[x].Dead = true; ;
                }
                else
                {
                    player.Kill();
                }
            }
        }
        else
        {
            if (!enemies[x].Enabled)
            {
                enemies.RemoveAt(x);
            }
        }
    }
  2. In the Player class, add a declaration to hold the lives the player has remaining:
    private int livesRemaining  = 3;
  3. Still in the Player class, add a property to ...

Get XNA 4.0 Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.