Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

JavaScript 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.

https://w.trhou.se/cqn2m32men

Steven Parker
Steven Parker
215,972 Points

We'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.

Sorry, I forgot to leave the snapshot: https://w.trhou.se/cqn2m32men

Ahh ok yes, thank you!

1 Answer

Steven Parker
Steven Parker
215,972 Points

The 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.

Thank 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
Steven Parker
215,972 Points

The 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.

Yay! 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
Steven Parker
215,972 Points

Looks 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)"