java - 我一直尝试运行我的程序,但我的一个类(class)不断向我发送此错误 "d.r cannot be resolved or is not a field"

标签 java processing

我一直尝试运行我的程序,但我的一个类不断向我发送此错误

"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/

相关文章:

java - "Syntax error on token "模块 ", interface expected"

python - 如何在 python 中的字符串(句子)中打印函数返回的字典输出? (加工)

java - 仅当使用自定义 Vector 类时,添加负零浮点结果才为 1.0f

javascript - 模糊 Canvas 元素?

Java LDAP - 将组添加到用户问题 - 错误代码 53 - WILL_NOT_PERFORM

java - 为什么使用相似种子时初始随机数相似?

java - 如何将 JSON 数组映射/反序列化为 POJO 或 POJO 列表?

java - 无法扫描字符串或整数

processing - 在 p5js 中创建星星的数学原理是什么

java - 处理导出项目需要java运行环境,而我已经有一个