Unexpected Token Error (Unaware of Cause)

var sprite = createSprite(40,200);
sprite.setAnimation("greenAlien");
var spriteoriginalposx = sprite.x;
var spriteoriginalposy = sprite.y;
var timer = 80; //timer used to limit how long sprite walks across screen
function draw() { //my lazy way of making a loop
  timer = (timer - 1); //timer count down
  background("orange");
  if (timer > 0){ //checking if timer has reached 0/ended
    sprite.rotation = randomNumber(-10, 10);
    if(sprite.x > 380){ //sets the sprite back to original spot when it reaches the end of screen
      sprite.x = spriteoriginalposx; // ↑↑↑↑
    } else { //if the sprite is not at the end of screen
      sprite.x = (sprite.x + 10); // makes the sprite "walk" in increments of 10
      sprite.rotation = 0;
      for (let CurrentXpos = sprite.x; sprite.x === 200; CurrentXpos = sprite.x){ // loops until sprite is at the middle of the screen
        sprite.x + 10; //moves the sprite until the for loop above ends ↑
      }
    }
  }
  drawSprites(); // creates each frame of the sprite movement
}

When I run this code in game lab I get the error
“ERROR: Line: 18: SyntaxError: Unexpected token (18:15)”

I’m quite clueless on what the cause of this is and I tried double checking my brackets and semicolons but I didn’t notice anything that would cause the code not to work, maybe someone here will see what’s wrong

I don’t think this matters very much, all I want is for the code to run, but it’s supposed to make the sprite walk across the screen a couple times, then walk to the middle, and stop.

Thanks for reading!

Hello @quazart9230. Welcome to the forum.

These are always easier to debug if you click on the share button and send us the shared link. Your screenshot is missing some of the right side of the code and since we don’t have access to your assets such as the sprites, testing the code and reproducing the error are hard to do. The screenshot also doesn’t give us the line numbers which would be helpful to find which line is throwing the error.

In this case, though, just based on looking at the code, I am guessing it could be your for loop. You are using a let clause and I don’t think let is supported in GameLab as GameLab is built on an earlier version of javascript. Try using var

Let us know if that’s it and if not, please send us the share link and we can look closer.

Cheers!

Mike

2 Likes

Thank you Mike, it worked!

Changing let to var fixed the issue!

Not trying to be rude but this knowledge may benefit you in future: I didn’t send a screenshot, it was a code embed made by putting code between 2 sets of triple back ticks, the main purpose of it is syntax highlighting, and making code look more neat on websites when placed near text.

Have a nice day, and thanks again!

I didn’t notice the embed… now I see there’s a scroll bar.

Glad it worked.

Mike