Unexpected token void, how to fix?

Idk why it’s giving me this unexpected error, can anyone help? quite new to programming :)) Thanks!

int left,right,up,down;
boolean goLeft,goRight,goUp,goDown;
int dudeX=1800,dudeY=1000, speed=1;

int chestX=1800,chestY=1000;

void setup(){
  noStroke();
  fullScreen();
  //replace with dungeon floor thingy
  background(50,50,50);
}


void draw (){
  if(dudeX + 20 >= 100)goLeft = false;     // r1 right edge past r2 left
  if(dudeX <= 100 + 100)goRight = false;     // r1 left edge past r2 right
  if(dudeY + 20 >= 150)goDown = false;     // r1 top edge past r2 bottom
  if(dudeY <= 150 + 100)goUp = false;     // r1 bottom edge past r2 top
  
  if(goUp)dudeY -= up * speed;
  if(goDown)dudeY += down * speed; 
  if(goLeft)dudeX -= left * speed;
  if(goRight)dudeX += right * speed;
  
  noCursor();
clear();
rect(chest,chestX,chestY,50,50);
rect(dude,dudeX,dudeY,100,100);
//Movement and exit
void keyPressed(){
  if (key == ESC){
   exit(); 
  }
  
  if (key == 'a' || key == 'A') {
    left = 1;
  }
  if (key == 'd' || key == 'D') {
    right = 1;
  }
  if (key == 'w' || key == 'W') {
    up = 1;
  }
  if (key == 's' || key == 'S') {
    down = 1;
  }
}
void keyReleased(){
  if (key == 'a' || key == 'A') {
    left = 0;
  }
  if (key == 'd' || key == 'D') {
    right = 0;
  }
  if (key == 'w' || key=='W') {
    up = 0;
  }
  if (key == 's' || key == 'S') {
    down = 0;
  }
}
1 Like

Hi,

The error you get is usually because you forgot a bracket or a semicolon.

When in processing IDE, hit ctrl+t to auto format. It is then easier to find those kind of mistakes. In your case you forgot a closing curly bracket before the keyPressed() function.

Also your rect() function are wrong.

int left, right, up, down;
boolean goLeft, goRight, goUp, goDown;
int dudeX=1800, dudeY=1000, speed=1;

int chestX=1800, chestY=1000;

void setup() {
  noStroke();
  fullScreen();
  //replace with dungeon floor thingy
  background(50, 50, 50);
}

void draw () {
  if (dudeX + 20 >= 100)goLeft = false; // r1 right edge past r2 left
  if (dudeX <= 100 + 100)goRight = false; // r1 left edge past r2 right
  if (dudeY + 20 >= 150)goDown = false; // r1 top edge past r2 bottom
  if (dudeY <= 150 + 100)goUp = false; // r1 bottom edge past r2 top

  if (goUp)dudeY -= up * speed;
  if (goDown)dudeY += down * speed;
  if (goLeft)dudeX -= left * speed;
  if (goRight)dudeX += right * speed;

  noCursor();
  clear();
  rect( chestX, chestY, 50, 50);
  rect( dudeX, dudeY, 100, 100);
}
//Movement and exit
void keyPressed() {
  if (key == ESC) {
    exit();
  }

  if (key == 'a' || key == 'A') {
    left = 1;
  }
  if (key == 'd' || key == 'D') {
    right = 1;
  }
  if (key == 'w' || key == 'W') {
    up = 1;
  }
  if (key == 's' || key == 'S') {
    down = 1;
  }
}
void keyReleased() {
  if (key == 'a' || key == 'A') {
    left = 0;
  }
  if (key == 'd' || key == 'D') {
    right = 0;
  }
  if (key == 'w' || key=='W') {
    up = 0;
  }
  if (key == 's' || key == 'S') {
    down = 0;
  }
}

PS : Please edit your last post and format the code by clicking this icon: </>

1 Like