提问者:小点点

错误:速度(向量)无法解析或不是字段


当我编译我的代码时,我收到一个错误,说速度(向量)无法解析或不是字段。有人对导致此错误的原因有什么建议吗?

PVector gravity;
PVector wind;
PVector friction;
Ball b;

void setup(){
    fullScreen();
    b=new Ball();
}

void draw() {
    background(240, 123, 50);
    b.update();
    //applying gravity to ball
    gravity=new PVector(0, .981);
    gravity.mult(mass);
    b.applyForce(gravity);
    //apply wind
    wind=new PVector (5, 0);
    b.applyForce(wind);
    //apply friction

下面一行是发生错误的地方。

    friction=b.velocity.get();
    friction.normalize();
    float c=-0.01;
    friction.mult(c);
    b.applyForce(friction);
    b.bounce();
    b.display();
}
PVector location;
PVector velocity;
PVector acceleration;
float mass, diam;

class Ball {
    Ball() {
        location=new PVector(width/2, height/2);
        velocity=new PVector(0, 0);
        acceleration=new PVector(0, 0);
        mass=5;
        diam=mass*20;
    }

    void update() {
        velocity.add(acceleration);
        location.add(velocity);
        acceleration.mult(0);
    }

    void applyForce(PVector force){
        PVector f=PVector.div(force,mass); 
        acceleration.add(f);
    }

    void bounce() { 
        if (location.y>=height-diam/2) {
            //hitting floor
            velocity.y*=-0.9;
            location.y=height-diam/2;
        } else if (location.y<=0) {
            //striking top
            location.y=0+diam/2;
            velocity.y*=-0.9;
        }
        if (location.x<=0+diam/2) {
            //hitting left
            location.x=0+diam/2;
            velocity.x*=-.9;
        } else if (location.x>=width-diam/2) {
            //hitting right
            location.x=width-diam/2;
            velocity.x*=-.9;
        }
    }

    void display() {
        ellipse(location.x, location.y, diam, diam);
    }
}

感谢任何帮助。


共1个答案

匿名用户

问题是速度不是Ball字段,它需要在Ball类中才能工作。

尝试这样做:

class Ball {
    PVector location;
    PVector velocity;
    PVector acceleration;
    float mass, diam;
    Ball() {
        ...
    }
    ...
}

而不是:

PVector location;
PVector velocity;
PVector acceleration;
float mass, diam;
class Ball {
    Ball() {
        ...
    }
    ...
}

唯一不同的是,我在课堂上包含了5个字段。