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

Doron Geyer
seal-mask
.a{fill-rule:evenodd;}techdegree
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 Points

Program freezes when I touch the third poison vial.

Hey guys, So I managed to get a life counter setup, however as soon as I touch the third poison vial the game freezes. I have tried several things but this is my first attempt at coding and after about an hour of tinkering I'm not too sure where to go .

I feel that my function on line 129 being a duplicate may be causing an issue. linked to my code in line 169.

my though process was as follows. create the variable deathMessage. mimic the function used for winning message that gives placement on the screen. ( I worked on the basis that you either win or lose so both having the same position should be fine as only one would be called )

I then created an if statement that says if lives=== 0 deathMessage should be called and text should be shown saying GAME OVER.

It is possible that I missed something in the videos explaining this process.

Would be grateful for any assistance, with a good explanation

https://w.trhou.se/vqbh7r62yr

Doron Geyer
seal-mask
.a{fill-rule:evenodd;}techdegree
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 Points

Dave StSomeWhere I have had a look at console, see the error you are talking about, however I replicated the code for winning message except for this bit here, that is the only difference.

what within this has given me the undefined function? // when the player wins the game if (won) { winningMessage.text = "YOU WIN!!!"; } if (lives ==0 ){ deathMessage.text = " GAME OVER!"; }

as all other code is identical between the 2 functions ( obviously excluding naming)

Thanks again for the feedback.

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

The issue is with your itemHandler() function (line 67) - maybe those two if statements at the end don't need to be there - what happens if you comment them out - or maybe create the createBadge() and deathMessage() functions?

// when the player collects an item on the screen
function itemHandler(player, item) {
  item.kill();
  if (item.key === 'coin') {
     currentScore = currentScore + 10;
  } else if (item.key === 'poison') {
     lives = lives - 1;
  } else if (item.key === 'star') {
     currentScore = currentScore + 20;
  }
  if (currentScore === winningScore) {
      createBadge();
  }
  if (lives == 0) {
      deathMessage();
  }
}
Doron Geyer
seal-mask
.a{fill-rule:evenodd;}techdegree
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 Points

Dave StSomeWhere as per your suggestions , I commented out the 2 if statements, and same result, once I get to the third poison vial the game freezes,

I think the issue is something I havent learnt yet ( this is my first attempt at programming ) I will continue with the techdegree course and perhaps revisit it in a week or 2 and see if the issue is possibly a bit clearer then.

thanks again for the help.

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Sounds good, just remember to enjoy the debugging - it is the fun part...

Another good debugging tool is the console.log() method

When I use the following code - it doesn't freeze on the third poison vial:

function itemHandler(player, item) {
  item.kill();
  if (item.key === 'coin') {
     currentScore = currentScore + 10;
  } else if (item.key === 'poison') {
     lives = lives - 1;
  } else if (item.key === 'star') {
     currentScore = currentScore + 20;
  }
  if (currentScore === winningScore) {
      console.log(' calling createBadge()');   // console.log here
  }
  if (lives == 0) {
      console.log(' calling deathMessage()');  // console.log here
  }
}

good luck and enjoy :tropical_drink:

Steven Parker
Steven Parker
229,608 Points

You folks might want to add your posts as answers instead of comments, so they can be voted on and possibly be choosen as "best answer" by Doron.

1 Answer

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Check your console for errors - you should see that you attempting to call an undefined function deathMessage() on line 81.