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

Jesse Thompson
Jesse Thompson
10,684 Points

How do I add lives? Do I just change the values of the currentScore and winningScore? Or do I have to call new variables

Im not sure how to make a 3 life system. I used ctrl f to find some type of "lives" to find some type of variable to build a separate system, but I cant find anything.

Hi Jesse - it's likely calling for you to add extra logic to the program to give it this functionality.

It might look similar to a function demonstrated in the preceding video at 1:10 (https://teamtreehouse.com/library/control-the-flow-with-conditional-statements). The player would I guess start with 3 lives (this figure being held in a variable) and then when something happens to cause the player to lose a life this runs the extra function you created which alters the lives variable from 3 to 2.

3 Answers

I also don't get it... I get how to make it so that hitting poison subtracts a life, but not how to get the lives counter in there in the first place.

What I did was I added new variable on the top.

var lives = 3;

Nikola Jankovic
Nikola Jankovic
10,793 Points

Hello, if I this can be helpful...

  • I have added new var named currentLifes
  • I have added in create function this: loosingMessage= game.add.text(game.world.centerX, 275, "", { font: "bold 48px Arial", fill: "white" }); loosingMessage.anchor.setTo(0.5, 1);
  • I have added this in a update function : text.text = "SCORE: " + currentScore +" LIFES: "+ currentLifes;
  • I added this function: //when the player loose lifes function looseLifes() { loose= true }
  • I have added this function inside itemHandler function: if (currentLifes === 0) { looseLifes(); }
Óscar Hernández sandoval
Óscar Hernández sandoval
9,257 Points

bewteern line 71 -78 there is a function poison.kills below this function add + sign to adding lives lives = lives + 1;