-1

I am new to java and I am using processing. I am just learning to how use classes is java and I am getting confusing error messages when I run a method. the error message is 'unexpected token: (' the error as at the p.setPieces(pawn, white); line

here is my code:

int ranks = 8;
int files = 8;
int spacing;

// set the values for all the pieces and colors
int empty = 0;
int pawn = 1;
int knight = 2;
int bishop = 3;
int rook = 4;
int queen = 5;
int king = 6;

int white = 8;
int black = 16;

Piece p = new Piece();
p.setPiece(pawn, white);

void setup() {
  size(600, 600);
  spacing = width / ranks;
}
 
void draw() {
  background(0);

  // draw the board
  for (int i = 0; i < ranks; i++) {
    for (int j = 0; j < files; j++) {
      if ((i + j) % 2 == 0) {
        noStroke();
        fill(255);
        rect(i * spacing, j * spacing, spacing, spacing);
      } else {
        noStroke();
        fill(0);
        rect(i * spacing, j * spacing, spacing, spacing);
      }
    }
  }
}

and then in a different file I have:

class Piece {

  // make variables for color and type of a piece
  int pieceType;
  int pieceColor;
  
  // set up type and color
  void setPiece(int Type, int Color) {
  
  pieceType = Type;
  pieceColor = Color;
  
  
  }
}
George Profenza
  • 50,687
  • 19
  • 144
  • 218
ilan
  • 17
  • 4
  • 2
    You can't call `p.setPiece(pawn, white);` directly in a class. It should be in a method or constructor or initialiser block. – khelwood Jun 24 '21 at 15:21
  • And `void setup() {` is never closed - or too late, you cannot have `void draw() {` within it. – luk2302 Jun 24 '21 at 15:22
  • its not in a class it is in the main file the class is in a different file. – ilan Jun 24 '21 at 15:22
  • void setup wasn't closed because I made a typo while copying and now that you pointed it out I fixed it – ilan Jun 24 '21 at 15:23
  • 1
    `p.setPiece(pawn, white);` cannot be before `void setup() {`, it cannot be outside a method (as khelwood already said) – luk2302 Jun 24 '21 at 15:35

1 Answers1

1

As khelwood and luk2302 mentioned, simply move p.setPiece(pawn, white); in setup() (preferably after size()):

int ranks = 8;
int files = 8;
int spacing;

// set the values for all the pieces and colors
int empty = 0;
int pawn = 1;
int knight = 2;
int bishop = 3;
int rook = 4;
int queen = 5;
int king = 6;

int white = 8;
int black = 16;

Piece p = new Piece();

void setup() {
  size(600, 600);
  spacing = width / ranks;
  p.setPiece(pawn, white);
}
 
void draw() {
  background(0);

  // draw the board
  for (int i = 0; i < ranks; i++) {
    for (int j = 0; j < files; j++) {
      if ((i + j) % 2 == 0) {
        noStroke();
        fill(255);
        rect(i * spacing, j * spacing, spacing, spacing);
      } else {
        noStroke();
        fill(0);
        rect(i * spacing, j * spacing, spacing, spacing);
      }
    }
  }
}

class Piece {

  // make variables for color and type of a piece
  int pieceType;
  int pieceColor;
  
  // set up type and color
  void setPiece(int Type, int Color) {
  
  pieceType = Type;
  pieceColor = Color;
  
  
  }
}

When using "active" mode (e.g. setup()/draw()) you can only declare variables (at the top), but not use them directly in the main block of code. You need to reference them within a function.

George Profenza
  • 50,687
  • 19
  • 144
  • 218