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 Introducing JavaScript Finishing the Game Wrapping It Up

Game freezes when collecting coins

Everything was working fine until I edited the bit of code involving conditions. Now when I run the game and collect the first code, the game freezes.

Here's the workspace: https://w.trhou.se/dav0ohmwwz

1 Answer

If you check the console you will see at line 87 you are treating losingMessage as if it was a function by following it with parentheses. This causes the crash. In addition this code always executes because you are using an assignment operator in your condition in the line before

  } if (lost = true) {

I updated your code here as follows

  } if (lost === true) {
        // losingMessage();
        losingMessage.text = "YOU LOSE, TRY AGAIN!!!";
  }

but you may want to add a function that kills the player/stops the game

You also aren't updating current lives onscreen where the player can see. This is because you have you update text twice. For this I added another variable text2

var text;
var text2;

modified the code that adds the text to the game

text = game.add.text(16, 16, "LIVES: " + currentLives, { font: "bold 24px Arial", fill: "white" });
text2 = game.add.text(650, 16, "SCORE: " + currentScore, { font: "bold 24px Arial", fill: "white" });

and the code the updates the text

  function update() {
    text.text = "LIVES: " + currentLives;
    text2.text = "SCORE: " + currentScore;