JavaScript

Doron Geyer
Doron Geyer
Full Stack JavaScript Techdegree Student 754 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

🍹 Dave 🌴 StSomeWhere
🍹 Dave 🌴 StSomeWhere
17,003 Points

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

Doron Geyer
Doron Geyer
Full Stack JavaScript Techdegree Student 754 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
17,003 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
Doron Geyer
Full Stack JavaScript Techdegree Student 754 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
17,003 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
148,790 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.