Unexpected token Void,

Hello, I'm having trouble troubleshooting errors in my code. Processing says that there is an "unexpected token: void" but I can't seem to find the problem. It point to line 47 in the following code but I'm not sure why it's a problem.

//First declare objects used for this session
Blocker obstacleBlocktopdown; 
Blocker obstacleBlockacross;
Avatar playerAvatar;
float move = 1;//?? needed?
//Player Avatar;
//Player Seeker; 
boolean upPressed = false;
boolean downPressed = false;
boolean leftPressed = false;
boolean rightPressed = false;


    void setup(){
      size(1300, 900); 
      //Initialize object here
      obstacleBlocktopdown = new Blocker(width/2, 1, 20, 20, 0, 2); 
      obstacleBlockacross = new Blocker(1, height/2, 20, 20, 3, .4);
      playerAvatar = new Avatar(0, 0, 15, 15);  
      //Avatar = new Player(color(255,0,0),10,100,2);
      //Seeker = new Player(color(0,0,255),100,10,1);
    } 

    void draw(){ 

      if (upPressed) {// playerAvatar moves up 1 pixel
       playerAvatar.movesup();
      }
      if (downPressed) {//playerAvatar moves down 1 pixel
       playerAvatar.movesdown();
      }
      if (leftPressed) {//playerAvatar moves to the left 1 pixel
       playerAvatar.movesleft();
      }
      if (rightPressed) {//playerAvatar moves to the right 1 pixel
       playerAvatar.movesright();
      }

      background(255); 
      //obstacleBlocktopdown.display();
      //obstacleBlockacross.display(); 
      obstacleBlocktopdown.run(); 
      obstacleBlockacross.run();
      playerAvatar.display(); 

        //enacts the movement of desired object
        void keyPressed (KeyEvent e) { 
      if (key == CODED) {
        if (keyCode == UP) {
          upPressed = true;
        }
        else if (keyCode == DOWN) {
          downPressed = true;
        }
        else if (keyCode == LEFT) {
          leftPressed = true;
        }
        else if (keyCode == RIGHT) {
          rightPressed = true;
        }
      }
    }

         // stops movement once key(s) are released 
    void keyReleased(KeyEvent e) {
      if (key == CODED) {
        if (keyCode == UP) {
          upPressed = false;
        }
        else if (keyCode == DOWN) {
          downPressed = false;
        }
        else if (keyCode == LEFT) {
          leftPressed = false;
        }
        else if (keyCode == RIGHT) {
          rightPressed = false;
        }
      }
    } 
     /* Avatar.drive();
      Avatar.display();
      Seeker.drive();
      Seeker.display();*/
    }

//I don't think my other classes are creating the problem but here they are for reference.

class Avatar {
  float x; 
  float y; 
  float a;
  float b; 

  //constructors 
  Avatar(){
  }

  Avatar (float _x, float _y, float _a, float _b) {
    x = _x;
    y = _y; 
    a = _a;
    b = _b; 
  }

  //Functions 
  void display () {
    stroke(0); 
    fill(83, 125, 243);
    ellipse(x, y, a, b);
  }

  void movesup(){
   y = y - 1; 
  playerAvatar.display(); 
  }
  void movesdown() {
    y = y + 1; 
    playerAvatar.display(); 
  }
  void movesleft() {
    x = x - 1; 
    playerAvatar.display(); 
  }
  void movesright() {
    x = x + 1; 
    playerAvatar.display(); 
  }

}

// ANOTHER CLASS

class Blocker {
  //Global variables
  float x = 0;
  float y = 0;
  float a = 0;
  float b = 0;
  float speedX = 0;
  float speedY = 0;

  //Constructors
  Blocker() {
  }

  Blocker(float _x, float _y, float _a, float _b, float _c, float _d) {
    x=_x;
    y=_y;
    a=_a;
    b=_b;
   // speedX = random(-4, 4);
    //speedY = random(-4, 4);
   speedX = _c;
   speedY = _d; 
  }

  //Functions
  void display () {
    ellipse (x, y, a, b);
  }

  void movement(float spX, float spY) {//make blockers move in basic motions

    x = x + speedX;
    y = y + speedY;
  }

  void bounce() {
    if (x > width) {
      speedX = speedX * -1;
    }
    if (x < 0) {
      speedX = speedX * -1;
    }
    if (y > height) {
      speedY = speedY * -1;
    }
    if (y < 0) {
      speedY = speedY * -1;
    }
  }

  void run() {
    //display();
    obstacleBlocktopdown.movement(0, 2);
    obstacleBlockacross.movement(3, .4);
    bounce();
    obstacleBlocktopdown.display();
    obstacleBlockacross.display();
    }

}

Any help will be appreciated.

Answers

  • Ctrl-t

    Where does your draw() function end?

  • KeyPressed and KeyReleased should be outside of the draw() function.

  • Yes that works. Those methods shouldn't be in my draw() function. Thank you

  • One more question though, I'm trying to make constraints for the playerAvatar object so it doesn't go off screen. Here is the code I implemented. I hope I'm using the constrain method correctly...

    //First declare objects used for this session
    Blocker obstacleBlocktopdown; 
    Blocker obstacleBlockacross;
    Avatar playerAvatar;
    boolean upPressed = false;
    boolean downPressed = false;
    boolean leftPressed = false;
    boolean rightPressed = false;
    
    
    void setup(){
      size(1300, 900); 
      //Initialize object here
      obstacleBlocktopdown = new Blocker(width/2, 1, 20, 20, 0, 2); 
      obstacleBlockacross = new Blocker(1, height/2, 20, 20, 3, .4);
      playerAvatar = new Avatar(11, 11, 15, 15);  
    } 
    
    void draw(){ 
       playerAvatar.constrain(playerAvatar.y, 10, height-10);
       playerAvatar.constrain(playerAvatar.x, 10, width-10);
    
      if (upPressed) {// playerAvatar moves up 1 pixel
       playerAvatar.movesup();
      }
      if (downPressed) {//playerAvatar moves down 1 pixel
       playerAvatar.movesdown();
      }
      if (leftPressed) {//playerAvatar moves to the left 1 pixel
       playerAvatar.movesleft();
      }
      if (rightPressed) {//playerAvatar moves to the right 1 pixel
       playerAvatar.movesright();
      }
    
      background(255); 
      obstacleBlocktopdown.run(); 
      obstacleBlockacross.run();
      playerAvatar.display(); 
    
    }
    
      //enacts the movement of desired object
        void keyPressed (KeyEvent e) { 
      if (key == CODED) {
        if (keyCode == UP) {
          upPressed = true;
        }
        else if (keyCode == DOWN) {
          downPressed = true;
        }
        else if (keyCode == LEFT) {
          leftPressed = true;
        }
        else if (keyCode == RIGHT) {
          rightPressed = true;
        }
      }
    }
    
         // stops movement once key(s) are released 
    void keyReleased(KeyEvent e) {
      if (key == CODED) {
        if (keyCode == UP) {
          upPressed = false;
        }
        else if (keyCode == DOWN) {
          downPressed = false;
        }
        else if (keyCode == LEFT) {
          leftPressed = false;
        }
        else if (keyCode == RIGHT) {
          rightPressed = false;
        }
      }
    } 
    

    I get an error saying "The function constrain(float, int, int) does not exist. I'm really new at this so it doesn't make much sense to me. I thought the syntax for the constrain() function was constrain(amt, low, high) where the amt refers to the value to be constrained? And it can return an int or float?

  • edited March 2015

    When we use the dot . operator, Java considers its right operand as a member of the left operand's object:
    https://processing.org/reference/dot.html

    • So at playerAvatar.constrain(playerAvatar.x, 10, width-10);
    • playerAvatar is the left operand of the dot . access operator and represents the object.
    • While constrain() is the right operand and it is assumed to be a member of playerAvatar.
    • Since variable playerAvatar is of type Avatar, the search's gonna happen inside that class.
    • And since there's no member called constrain() there, it's gonna display it doesn't exist!
    • Same thing for playerAvatar.x. Only diff. is that the member x actually exists there!
    • However, if you had just used Processing API's constrain(), it'd surely be found!

    https://processing.org/reference/constrain_.html

  • edited March 2015 Answer ✓

    Much probably what you meant was assigning the result of constrain() back to field x: ;;)
    playerAvatar.x = constrain(playerAvatar.x, 10, width-10);

    It'd be much better if that was an Avatar's method instead: *-:)

    void constrainToScreen() {
      x = constrain(x, a/2.0, width  - a/2.0);
      y = constrain(x, b/2.0, height - b/2.0);
    }
    
  • edited March 2015

    Also take a look at my online example below:
    http://studio.processingtogether.com/sp/pad/export/ro.91tcpPtI9LrXp/

    The class Player there also relies on constrain() @ its member method move()! :bz

  • Hi, "GoToLoop", there is a slight problem with the constrainToScreen method. It makes the left and right buttons make the playerAvatar go on a diagonal and the moveup() and movedown() methods don't work. I'm trying to tweak it but I'm still running into problems.

    And on an unrelated note, I can't control the color of some objects. I wanted to change the color of the Avatar object but it changed the color of all objects on screen. Why is that? The code is below...

    class Avatar {
      float x; 
      float y; 
      float a;
      float b; 
    
      //constructors 
      Avatar(){
      }
    
      Avatar (float _x, float _y, float _a, float _b) {
        x = _x;
        y = _y; 
        a = _a;
        b = _b; 
      }
    
      //Functions 
      void display () {
        stroke(0); 
        fill(83, 125, 243);
        ellipse(x, y, a, b);
      }
    
      void movesup(){// MAY NEED IF ELSE STATEMENTS 
        constrain(y, 10, height-10);
       y = y - 1; 
      playerAvatar.display(); 
      }
      void movesdown() {
        constrain(y, 10, height-10);
        y = y + 1; 
        playerAvatar.display(); 
      }
      void movesleft() {
        constrain(x, 10, width-10);
        x = x - 1; 
        playerAvatar.display(); 
      }
      void movesright() {
        constrain(x, 10, width-10);
        x = x + 1; 
        playerAvatar.display(); 
      }
    
      void constrainToScreen() {
      x = constrain(x, a/2.0, width  - a/2.0);
      y = constrain(x, b/2.0, height - b/2.0);
    }
    
    }
    

    // THE BLOCKER CLASS

    class Blocker {
      //Global variables
      float x = 0;
      float y = 0;
      float a = 0;
      float b = 0;
      float speedX = 0;
      float speedY = 0;
    
      //Constructors
      Blocker() {
      }
    
      Blocker(float _x, float _y, float _a, float _b, float _c, float _d) {
        x=_x;
        y=_y;
        a=_a;
        b=_b;
       // speedX = random(-4, 4);
        //speedY = random(-4, 4);
       speedX = _c;
       speedY = _d; 
      }
    
      //Functions
      void display () {
        ellipse (x, y, a, b);
      }
    
      void movement(float spX, float spY) {//make blockers move in basic motions
    
        x = x + speedX;
        y = y + speedY;
      }
    
      void bounce() {
        if (x > width) {
          speedX = speedX * -1;
        }
        if (x < 0) {
          speedX = speedX * -1;
        }
        if (y > height) {
          speedY = speedY * -1;
        }
        if (y < 0) {
          speedY = speedY * -1;
        }
      }
    
      void run() {
        //display();
        obstacleBlocktopdown.movement(0, 2);
        obstacleBlockacross.movement(3, .4);
        bounce();
        obstacleBlocktopdown.display();
        obstacleBlockacross.display();
        }
    
    }
    

    // SETUP AND DRAW FUNCTIONS.

    //First declare objects used for this session
    Blocker obstacleBlocktopdown; 
    Blocker obstacleBlockacross;
    Avatar playerAvatar;
    boolean upPressed = false;
    boolean downPressed = false;
    boolean leftPressed = false;
    boolean rightPressed = false;
    
    
    void setup(){
      size(1300, 900); 
      //Initialize object here
      obstacleBlocktopdown = new Blocker(width/2, 1, 20, 20, 0, 2); 
      obstacleBlockacross = new Blocker(1, height/2, 20, 20, 3, .4);
      playerAvatar = new Avatar(11, 11, 15, 15);  
    } 
    
    void draw(){ 
      // playerAvatar.constrain(playerAvatar.y, 10, height-10);
      // playerAvatar.constrain(playerAvatar.x, 10, width-10);
      playerAvatar.constrainToScreen(); 
    
    
      if (upPressed) {// playerAvatar moves up 1 pixel
       playerAvatar.movesup();
      }
      if (downPressed) {//playerAvatar moves down 1 pixel
       playerAvatar.movesdown();
      }
      if (leftPressed) {//playerAvatar moves to the left 1 pixel
       playerAvatar.movesleft();
      }
      if (rightPressed) {//playerAvatar moves to the right 1 pixel
       playerAvatar.movesright();
      }
    
      background(255); 
      obstacleBlocktopdown.run(); 
      obstacleBlockacross.run();
      playerAvatar.display(); 
    }
    
      //enacts the movement of desired object
        void keyPressed (KeyEvent e) { 
      if (key == CODED) {
        if (keyCode == UP) {
          upPressed = true;
        }
        else if (keyCode == DOWN) {
          downPressed = true;
        }
        else if (keyCode == LEFT) {
          leftPressed = true;
        }
        else if (keyCode == RIGHT) {
          rightPressed = true;
        }
      }
    }
    
         // stops movement once key(s) are released 
    void keyReleased(KeyEvent e) {
      if (key == CODED) {
        if (keyCode == UP) {
          upPressed = false;
        }
        else if (keyCode == DOWN) {
          downPressed = false;
        }
        else if (keyCode == LEFT) {
          leftPressed = false;
        }
        else if (keyCode == RIGHT) {
          rightPressed = false;
        }
      }
    } 
    

    Why do all my objects change color?

  • Ok so I fixed the problem with the diagonal movement. (it turned out to be really simple, but I can't seem to get the colors right. Why do all my objects change to the same color?

  • edited April 2015 Answer ✓

    Why do all my objects change to the same color?

    • B/c fill(), stroke(), etc. alter the whole canvas' foreground color!
    • Anything drawn after any of them obeys the latest chosen color.
    • In short, those functions control the sketch's canvas properties.
    • And therefore they're global configs, affecting everywhere!

    I see that inside Avatar's display() method both stroke() & fill() are issued:

    void display () {
      stroke(0);
      fill(83, 125, 243);
      ellipse(x, y, a, b);
    }
    
    • Of course that's gonna affect Blocker's display() too and anything else! :-\"
    • Either issue fill() before calling Blocker's own display() or have that inside it as well! *-:)

    • I see that you define a fixed color for Avatar. So it's a constant!
    • In my online example, Player's colors are defined inside it as constant fields:

    static final color INK = #008000, OUTLINE = 0;

    • In your case that'd translate as:

    static final color INK = #537DF3, OUTLINE = 0;
    
    void display () {
      stroke(OUTLINE);
      fill(INK);
      ellipse(x, y, a, b);
    }
    
    • Do the same for the Blocker class if all of them have the same color properties.
    • In case each Blocker got its own color, you're gonna need regular color fields to store it.
    • That is, in the same way we store the other instance properties like coordinates, dimensions, etc.

    • One final tip: upPressed, downPressed, leftPressed & rightPressed would be better fit as Avatar's fields rather than sketch's! :>
    • Again take a look at how the online sketch tackles it:

    void keyPressed() {
      p.setMove(keyCode, true);
    }
    
    void keyReleased() {
      p.setMove(keyCode, false);
    }
    
    • It invokes Player's setMove() method, which either sets or resets its boolean fields.
    • Then Player's move() method calculates the movement based on those 4 boolean fields.
    • Of course you do almost the same but inside sketch's draw()! O:-)
Sign In or Register to comment.