我一直尝试运行我的程序,但我的一个类不断向我发送此错误
"d.r cannot be resolved or is not a field"
鹿应该是 Frog 类游戏的一部分,它们必须在不被汽车撞到的情况下到达路的一侧。每当鹿到达路另一边的路牌时,它们就会重生。
class Deer {
float r; // radius
float x, y; // location
color col;
Deer(float tempR) {
r = tempR;
x = 500;
y = 250;
col = color(50, 10, 10, 150);
}
void setLocation(float tempX, float tempY) {
x = 500;
y = 250;
}
void display() {
//moveYourDeer Right();
fill(255);
ellipse(x, y, r * 2, r * 2);
}
void moveYourDeerLeft() {
if (keyPressed) {
if (key == 'a' || key == 'A') {
r--;
}
}
}
void moveYourDeerRight() {
if (keyPressed) {
if (key == 's' || key == 'S') {
r--;
}
}
}
void moveYourDeerForward() {
if (keyPressed) {
if (key == 'w' || key == 'W') {
r++;
}
}
}
void moveYourDeerBackwards() {
if (keyPressed) {
if (key == 'z' || key == 'Z') {
r++;
}
}
}
boolean intersect(StreetSign d) {
// Calculate distance
float distance = dist(x, y, d.x, d.y);
// Compare distance to sum of radii
if (distance < r + d.r) {
return true;
}
else {
return false;
}
}
// If the deer is caught by sign
void caught() {
// Stop it from moving by setting speed equal to zero
speed = 0;
// Set the location to somewhere way off-screen
y = -1000;
}
}
最佳答案
尝试访问 Deer.r
的类不允许这样做。您应该将 r 公开:
class Deer {
public float r; // radius
(...)
或者为 r 创建一个 setter/getter ,然后使用 d.getR()
:
public float getR() {
return r;
}
关于java - 我一直尝试运行我的程序,但我的一个类(class)不断向我发送此错误 "d.r cannot be resolved or is not a field",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29198571/