Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Introducing JavaScript Finishing the Game Wrapping It Up

Stacy Chairez
Stacy Chairez
178 Points

How to create a lifeCounter

After this last video, the instructor suggests we create a lifeCounter that will decrease every time the player touches the poison. How do I do this? I used createItem to create a lifeCounter, but doesn't that somehow need to be linked to a png file so the game knows what to do? I also wrote var lives =3 under the define variables function, but nothing has changed when i view the game. I know I'm missing something here. Can I get some direction on this please?

However, I couldn't figure out yet how to actually substract the lives when hitting the poison.

Isn't this the right code (like described in the code challenge)?

// when the player collects an item on the screen
function itemHandler(player, item) {
  item.kill();
  if (item.key === 'coin') {
     currentScore = currentScore + 10;
  } else if (item.key === 'star') {
     currentScore = currentScore + 25;
  }


  function poisonCollect(player, poison) {
    poison.kill();
    lives = lives - 1;
    if (lives === 0) {
        player.kill();
        gameOver = true;
    }
}


  if (currentScore === winningScore) {
      createBadge();
  }
}

Hmm

Hi Stacy,

livesText = game.add.text(685, 16, "LIVES: " + lives, { font: "24px Arial", fill: "white" });

Use this instead of createItem. You don't need a graphic, it is just a text. This line gives us the text "LIVES" on the top right.

1 Answer

Steven Parker
Steven Parker
231,269 Points

This was the second task of the final code challenge. Take a look at the code in the challenge.