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 trialBaheejah Fareed
3,450 PointsJavaScript game troubleshooting poison control
Will you help me to spot what is wrong with my code? I am trying to make it so that when a poison is collected "LIVES" decreases by 1, but nothing is happening. I used examples from the challenge in that lesson.
Baheejah Fareed
3,450 PointsSorry, I forgot to leave the snapshot: https://w.trhou.se/cqn2m32men
Baheejah Fareed
3,450 PointsAhh ok yes, thank you!
1 Answer
Steven Parker
231,899 PointsThe lives are only reduced in the function "poisonHandler" on line 78, but that function is never called in the program. The function "itemHandler" just above it is called for interaction with any kind of item, so you could call it from there. But you could also just merge the logic into that function:
function itemHandler(player, item) {
item.kill();
if (item.key === 'coin') {
currentScore = currentScore + 10;
}
else if (item.key === 'poison') { // add a new case for a poison
lives = lives - 1;
if (lives === 0) {
player.kill();
gameOver = true;
}
}
}
This would also be a good place to add code if you wanted to give stars a value.
Also, on line 136 "livesText" should be "livesText.text" to update the life count on the screen.
Baheejah Fareed
3,450 PointsThank you so very much! I tried merging the lives initially in the item.handler, however missed a curly brace. I've got it working now thanks to your help.
Another feature that I wanted to add was adding lives when collecting stars. Is that possible? Here is what I have so far, but it's not working. https://w.trhou.se/4d5ii160t3
Steven Parker
231,899 PointsThe condition added for "star" is inside the code block for "poison", but it should come after it (after the closing brace currently on line 85).
Also, lines 137 and 138 are duplicates, you only need one of them.
Baheejah Fareed
3,450 PointsYay! I did it with your help! Tell me what you think. https://w.trhou.se/i3ea01r29b
I'm going to work on creating a losing message soon.
Steven Parker
231,899 PointsLooks good so far.
Just in case you might add something later that allows the score to be surpassed with a single item, you might want to change the win test to "(currentScore >= winningScore)
"
Steven Parker
231,899 PointsSteven Parker
231,899 PointsWe'd need to see the code. If you post a question with the "get help" button, it should add the code and a link to the page for you.