-2

主要课程:

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();
}

警卫级

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);
}

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

4

1 回答 1

1

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

class Guard {
  ...
}

void init() {

}

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

于 2013-10-23T19:37:36.700 回答