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

Wayne Topp
Wayne Topp
948 Points

Where did I go wrong in the Bonus Game?

I am very new to coding and have never seen JavaScript code before. This Introduction to JavaScript was great and I enjoyed working on building (well, modifying) a game. I started playing around with the Bonus Game and I successfully changed the background color, the speed of the character, and the text for winning the game) but I wanted to add a coin to the game (so that you can actually win the game). When I went to preview the game, it doesn't seem to be working. Can someone tell me where I went wrong?

https://w.trhou.se/5o9ivub4h9

3 Answers

Neil McPartlin
Neil McPartlin
14,662 Points

Hi Wayne. Good on you for daring to go 'off-piste' early in your learning curve. I'm not that far ahead of you but continually working with code that simply works is not that great for learning, but when things go wrong, then the learning really starts :)

Here is what I found...

1: So as you say, in preview, nothing is visible, just a black page. The first thing to do is to open your browser's devtools JavaScript console. (e.g. F12 in Chrome). Here it says...

Uncaught SyntaxError: Unexpected end of input game.js:184

Now line 184 happens to be the last line of code in game.js. Although your Treehouse Workspace doesn't report errors in code files, many popular code editors do. I happen to use Visual Studio Code but there are other popular editors like ATOM etc. All of that said, my VSC simply confirms what the Chrome browser said, that the code is incomplete but it cannot show exactly where.

A nice feature in these editors (and in Treehouse Workspace), is that you can rest your mouse on an opening ( or [ or { and the editor will show you what it believes to be the matching closing bracket.

So long story short, I examined your game.js file and found 2 functions where the closing } were missing. Here is a code snippet showing them added...

function starCollect(player, star) {
    star.kill();
    currentScore = currentScore + 20;
    if (currentScore === winningScore) {
        won = true;
    }
} // Added

  function coinCollect(player, coin) {
    coin.kill();
    currentScore = currentScore + 40;
    if (currentScore === winningScore) {
        won = true;
    }
} // Added

2: So if you make and save the above changes, then refresh your browser, it still does not work. You do now see a blue background, but there are now a lot more error messages reported in the JavaScript console. Don't panic. It can be that just one small error creates several other error messages. Let's look at the first one.

Uncaught TypeError: Cannot read property 'create' of undefined game.js:64

Line 64 is...

var coins = coins.create(left, top, coinImage);

Seeing 'coins' on both sides of the variable declaration looked odd, and sure enough, looking at the other functions nearby, it is apparent that the declaration should actually be 'var coin = ' i.e.

    var coin = coins.create(left, top, coinImage);

So make this change, save, refresh your browser and bingo, all should be good.

In your console, there should be no more red error messages, just yellow warning messages referring to issues with the phaser.js file concerning timing violations and deprecated code usage.

Wayne Topp
Wayne Topp
948 Points

Thank you so much both for the resources and for finding the errors. I'm glad to see they were "minor" errors, but I'm amazed at how something as simple as a bracket or a single letter changes completely the way the code is read.

Neil McPartlin
Neil McPartlin
14,662 Points

Happy to help Wayne. All were indeed minor typos and yet missing brackets and/or letters can completely confuse the JavaScript engine. It's a dumb 'machine' after all. Depending on context, you can get away with missing ' ; ' , but if the teacher uses them, I do too.

Please mark my response as 'Best Answer' so that others searching the 'Community' can see at a glance that a solution has been found.... plus I get 12 points ; -)

Wayne Topp
Wayne Topp
948 Points

Thanks for the reminder, Neil, that no errors are small when the engine can't run because of them. I'll keep that in mind.