处理错误:x无法解析或不是字段。希望能够点击所有的省略号

问题描述 投票:0回答:1

我在 java 模式下使用处理

我想做一个简单的游戏,鸭子(目前是椭圆)使用数组穿过屏幕,使用鼠标你必须点击所有的鸭子才能进入下一个级别。我目前正在尝试解决能够单击出现的所有省略号的问题。

我的完整代码是:

Bird b;

Bird [] birds;

int score;

void setup() {
  size (800, 600);

  b = new Bird();
  score = 0;

  birds = new Bird[4];

  for (int i=0; i<birds.length; i++) {
    birds[i] = new Bird();
  }
}

void draw() {
  background(200);

  b.update();
  b.display();
  
  //show mouse cursor
  fill(255, 0, 0, 100);
  ellipse(mouseX, mouseY, 10, 10);
  
  for (int i=0; i<birds.length; i++) {
    birds[i].update();
    birds[i].display();
  }
  fill(255);
  textSize(48);
  textAlign(LEFT);
  text("Score: " + score, 50, 50);
  
  float distanceFromClick = dist(mouseX, mouseY, birds.x, birds.y);
  
  if(mousePressed && distanceFromClick < 25) {
   score += 1; //increases score 
  }
}
 void mousePressed() {
 }
class Bird {
  //props
  float x, y, w, h, vx, vy;
    //Constructor
    Bird() {
    x = -100;
    y = random(100, 500);
    w = 50;
    h = 50;
    vx = 2;
    vy = 0;
  }
  //methods
  void update() {
    x += random(0, 2)*vx;
    y += vy;
  }
  void display () {
    //active area
    fill(255, 255, 0, 100);
    ellipse(x, y, w, h);
  }
}

我希望能够单击数组中的所有鸟而不是一只鸟,但是当我将 b.x 和 b.y 更改为 birds.x 和 birds.y 时,我收到错误消息“x 无法解析或不是字段”

在线浮动

distanceFromClick = dist(mouseX, mouseY, birds.x, birds.y);

java processing
1个回答
0
投票

你需要重新做你的构造函数看起来像这样:

  float x, y, w, h, vx, vy;
  //Constructor
  Bird(float xcoord, float ycoord, float wid, float ht, float vertx, float verty) {
    x = xcoord;
    y = ycoord;
    w = wid;
    h = ht;
    vx = vertx;
    vy = verty; 
  }

然后像这样初始化小鸟:

 b = new Bird(-100, random(100,500),50,50,2,0);

基于您最初的帖子的可运行代码: 这将被更改,因为您随后可以调试您的碰撞代码。

Bird b;

Bird [] birds;

int score;

void setup() {
  size (800, 600);

  b = new Bird(-100, random(100, 500), 50, 50, 2, 0);
  score = 0;

  birds = new Bird[4];

  for (int i=0; i<birds.length; i++) {
    birds[i] = new Bird(-100, random(100, 500), 50, 50, 2, 0);
  }
}

void draw() {
  background(200);

  b.update();
  b.display();

  //show mouse cursor
  fill(255, 0, 0, 100);
  ellipse(mouseX, mouseY, 10, 10);

  for (int i=0; i<birds.length; i++) {
    birds[i].update();
    birds[i].display();
  }
  fill(255);
  textSize(48);
  textAlign(LEFT);
  text("Score: " + score, 50, 50);

  float distanceFromClick = dist(mouseX, mouseY, b.x, b.y);
  println(distanceFromClick);
  if (mousePressed && distanceFromClick < 25) {
    score += 1; //increases score
    println(score);
  }
}

void mousePressed() {
}

class Bird {
  //props
  float x, y, w, h, vx, vy;
  //Constructor
  Bird(float xcoord, float ycoord, float wid, float ht, float vertx, float verty) {
    x = xcoord;
    y = ycoord;
    w = wid;
    h = ht;
    vx = vertx;
    vy = verty;
  }

  //methods
  void update() {
    x += random(0, 2)*vx;
    y += vy;
  }
  void display () {
    //active area
    fill(255, 255, 0, 100);
    ellipse(x, y, w, h);
  }
}

需要解决的问题:

您的代码总共初始化了五只鸟:除了 4 只鸟的数组之外还有一只鸟。为了简单和减少混淆,您可能想要摆脱这只鸟。然后你应该检查数组中的每只鸟在 mousePressed() 下而不是在 draw() 内与鼠标的接近程度。 draw() 函数每秒被调用 60 次,如果您在该函数中留下接近度代码,您的分数将会大大降低。如果将其移动到 mousePressed() 它将更准确。请记住通过检查坐标来检查数组中的每只鸟,例如 bird[i].x 和 bird[i].y.

© www.soinside.com 2019 - 2024. All rights reserved.