我在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更改为bird.x和bird.y时,我收到错误“x无法解析或不是字段”
在线浮点距离FromClick=dist(mouseX, mouseY,bird.x,bird.y);
你需要重新做你的构造函数,看起来像这样:
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);