U3, L17, Level 13: Need help with bug

csd-unit-3-lesson-17
#1

I have a student who is receiving the error message: Syntax Error: Unexpected Token (20:2) and we can’t figure out why he’s getting that error or how to fix it. It is always on Line 20 no matter what code is there. We’ve tried reloading the page, deleting the code and re-entering it, copying the code and repasting it into a restarted level, and logging out and back into Code.org. None of this has worked. I don’t see any other errors in his program that would cause this. He has a lot of code written, so I don’t want to have him start over. Any ideas?

#2

Joey,

Sometimes the error messages are misleading - if it’s at the beginning of the line, then often times you haven’t closed the previous line with a semi-colon. Care to share the code so we can check it out?

Brad

#3

I’ve checked that, but here is his code (it’s kind of long in this format) The bolded, italicized code is the line giving us the issue:

var character = createSprite(200, 200);
character.setAnimation(“flyer”);
var coinh = createSprite(randomNumber(0, 400), randomNumber(0, 400));
coinh.setAnimation(“coin”);
var ack = createSprite(randomNumber(0, 400), randomNumber(0, 400));
ack.setAnimation(“Bad,ol,RUSTYBOI”);
var bandaid = createSprite(randomNumber(0, 400), randomNumber(0, 400));
bandaid.setAnimation(“bandage_1”);
var coinh_2 = createSprite(randomNumber(0, 400), randomNumber(0, 400));
coinh_2.setAnimation(“coin”);
var ack_2 = createSprite(randomNumber(0, 400), randomNumber(0, 400));
ack_2.setAnimation(“Bad,ol,RUSTYBOI”);
var coinh_3 = createSprite(randomNumber(0, 400), randomNumber(0, 400));
coinh_3.setAnimation(“coin”);
var ack_3 = createSprite(randomNumber(0, 400), randomNumber(0, 400));
ack_3.setAnimation(“Bad,ol,RUSTYBOI”);
function draw()
background(“skyblue”)
// Simulating Gravity
if (keyDown(“left”)) {
character.velocityX = -5;
character.setAnimation(“flyer_copy_1”);
}
if (keyDown(“down”)) {
character.velocityY = 5;
character.setAnimation(“flyer_copy_1_copy_1”);
if (keyDown(“space”)) {
character.setAnimation(“BREATH”);
}
}
character.debug = true;
if (keyDown(“right”) != true)
character.setAnimation(“flyer”);
if (keyDown(“left”) != true)
character.setAnimation(“flyer”);
if (character.isTouching(coinh)) {
coinh.x = randomNumber(0, 400);
coinh.y = randomNumber(0, 400);
}
if (coinh.isTouching(ack)) {
coinh.x = randomNumber(0, 400);
coinh.y = randomNumber(0, 400);ack.x = randomNumber(0, 400);
ack.y = randomNumber(0, 400);
}
if (character.isTouching(ack)) {
ack.x = randomNumber(0, 400);
ack.y = randomNumber(0, 400);
character.setAnimation(“flyer_rusti_sad”);
ack.setAnimation(“EVIL”);
if (coinh.isTouching(ack)) {
coinh.x = randomNumber(0, 400);
coinh.y = randomNumber(0, 400);ack.x = randomNumber(0, 400);
ack.y = randomNumber(0, 400);
}
if (character.isTouching(ack)) {
ack.x = randomNumber(0, 400);
ack.y = randomNumber(0, 400);
character.setAnimation(“flyer_rusti_sad”);
ack.setAnimation(“EVIL”);
} else {
ack.setAnimation(“Bad,ol,RUSTYBOI”);
}
if (bandaid.isTouching(ack)) {
bandaid.setAnimation(“bandage_NOPE”);
ack.setAnimation(“EVIL_sad/disapointed”);
bandaid.x = randomNumber(0, 400);
ack.x = randomNumber(0, 400);
bandaid.y = randomNumber(0, 400);
ack.y = randomNumber(0, 400);
} else {
bandaid.setAnimation(“bandage_1”);
ack.setAnimation(“Bad,ol,RUSTYBOI”);
}
if (bandaid.isTouching(coinh)) {
bandaid.setAnimation(“bandage_1_copy_1”);
coinh.setAnimation(“coin_copy_1”);
bandaid.x = randomNumber(0, 400);
coinh.x = randomNumber(0, 400);
bandaid.y = randomNumber(0, 400);
coinh.y = randomNumber(0, 400);
} else {
coinh.setAnimation(“coin”);
bandaid.setAnimation(“bandage_1”);
}
if (bandaid.isTouching(character)) {
character.setAnimation(“flyer”);
bandaid.x = randomNumber(0, 400);
bandaid.y = randomNumber(0, 400);
}
drawSprites();
}
//i don’t even know what ack.x is doing right now in line 43, but i like it.

Here is an image of the code. No matter what is in that specific spot, it is highlighted in blue and the error code pops up.

#4

Could you give the link to the code instead? It makes it so much easier to debug rather than images. Thanks!

#5

Line 18 “background” needs a semi-colon.

1 Like
#6

@bradleywellsashley I tried that and got a yellow triangle saying “Unnecessary semicolon.” The error message still popped up on line 20. It doesn’t matter what code is on that line, it still says Unexpected Token (20:2).

@wadc_edavis Here’s the link. Hopefully we can figure this out. It’s so strange!

#7

Joey,

Moving back up through your code - Line 17 is the draw loop without the { }. Add those in and try. When I did it, it gave me errors on the Sprites, but nothing with the logic.

Brad

#8

Awesome! I think that did the trick. I had tried that before, but I didn’t put the closing brace as I thought there was one at the end of the program. The sprites issue is probably because I had him start over on the level and it wiped out his animations. Thank you! I’ll have him work on it today and see if it runs correctly.

1 Like
#9

Joey,

Awesome - let us know!

Brad