JavaScript Introducing JavaScript Finishing the Game Wrapping It Up

Jesse Thompson
Jesse Thompson
10,682 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.

Chris Mason
Chris Mason
Full Stack JavaScript Techdegree Graduate 24,246 Points

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.

Rachel Pohan
Rachel Pohan
Pro Student 595 Points

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,256 Points

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