JavaScript Introducing JavaScript Finishing the Game Wrapping It Up

changed sprite to mikethefrog and added life counter, but score stays at 0 doesn't change. help

https://w.trhou.se/htii2v880d

that is my 1st snapshot of my workspace.. i would like to know what i need to add or did wrong as i'm very interested in learning the basics and more to become a future web developer

3 Answers

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 50,920 Points

Both your Score and Life text elements are named text. So in the code you are setting the Life value to the Score value then immediately change it to the Life value, if that makes sense. Anyways the code I modified is as follows:

(1) Add another text variable at line 10. This will be text1 for Life.

var text;
var text1;

(2) Change text to text1 on line 129 for Life where both texts are created

text = game.add.text(16, 16, "SCORE: " + currentScore, { font: "bold 24px Arial", fill: "white" });
text1 = game.add.text(500, 16, "LIFE: " + currentLife, { font: "bold 24px Arial", fill: "white" });

(3) Change text to text1 on line 139 for Life where the value is set

 text.text = "SCORE: " + currentScore;
  text1.text = "LIFE: " + currentLife; 

Here is a snapshot with the modifications and functioning score;

https://w.trhou.se/zmnb1cjinj

thank you very much Kris! it now works flawlessly. now I am left with creating "game over" when you collect all 3 poisons.

Gabriel Pierce-Lackey
MOD
Gabriel Pierce-Lackey
Treehouse Moderator 10,993 Points

I would look at exactly what you just changed when you did your last save. I can't find exactly what is wrong, but JS is not my best. Retrace your steps one by one and redo your last changes one by one as well and see exactly what is throwing it off. One problem I did notice is that you have currentScore when you need currentLife in one spot.