处理错误:函数init()不存在

时间:2013-10-23 19:33:57

标签: java

MAIN CLASS:

Player thePlayer = new Player();
Guard theGuard = new Guard();

void setup() {
  size(1000, 500);
  theGuard.init();
  thePlayer.init();
}

void updateGame() {
  thePlayer.update();
}

void drawGame() {
  background(0);
  thePlayer.draw();

  fill(color(255, 255, 255));
  text("Score:" + score, 10, 20);
}

void draw() {
  fill (255, 255, 255);
  rect(0, 401, 1000, 100);
  noFill();

  updateGame();
  drawGame();
}

GUARD CLASS

class Guard {
  float x, y;
  float vx, vy;
  float fillColor;
  float diameter;
}

void init() {

}

void update() {
  if (x == (irandom(width)-100)) 
    vx = 3;

  if (x == (irandom(width)+100)) 
    vx = -3;

  x += vx;
  y += vy;
}

void draw() {
  fill(fillColor);
  rect(x, y, diameter, diameter);
}

球员类

class Player {
  float x, y;
  float vx, vy;
  float fillColor;
  float playerHeight, playerWidth;
  float jumpTime;
  boolean isJumping;
}

void init() {
  playerHeight = 20;
  playerWidth = 20;
  fillColor = color(255, 255, 255);
  jumpTime = 200;
  isJumping = false;

  x = 100;
  y = 400;

  vx = 0;
  vy = 0;
}

void update() {
  if (keyPressed) {
    if (keyCode == LEFT) vx = -2;
    if (keyCode == RIGHT) vx = 2;
    if (keyCode == UP) isJumping = true;
    if (keyCode == DOWN) playerHeight = 10;
  }
  else {
    vx = 0;
    vy = 0;
  }

  if (isJumping == true) {
    vy = -2;
  }
  else {
    vy -= 0.1;
  }

  if (y == 100)
    vy = 0;

  x += vx;
  y += vy;
}

void draw() {
  fill(fillColor);
  ellipse(x, y, playerWidth, playerHeight);
}

我在Processing(JAVA)中写了它。但我不知道为什么我的代码不起作用。当我尝试启动游戏时,它会给出错误:“函数init()不存在”。此功能在主类的第6/7行中使用。有没有办法创建这个功能?

1 个答案:

答案 0 :(得分:1)

注意定义类和方法的括号在这里。 Guard没有init()

class Guard {
  ...
}

void init() {

}

当然应该在init中定义Guard。可能应该在它自己的文件Guard.java

中定义Guard