你混淆了class 和Object的概念。
你的Mover是一个class,你需要在这个class下定义一个变量。
修改过的代码:
class ClassNameMover {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
ClassNameMover() {
mass=1;
location=new PVector( 30, 30);
velocity=new PVector (0, 0);
acceleration= new PVector(0, 0);
}
void applyForce(PVector force) {
PVector f = PVector.div(force, mass);
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void display() {
stroke(0);
fill(175);
ellipse(location.x, location.y, mass*16, mass*16);
}
void checkedge() {
if (location.x>width) {
location.x=width;
velocity.x*=-1;
} else if (location.x < 0) {
velocity.x *= -1;
location.x = 0;
}
if (location.y > height) {
velocity.y *= -1;
location.y = height;
}
}
}
PVector wind= new PVector (0.01, 0);
PVector gravity= new PVector (0, 0.1);
ClassNameMover Mover;
void setup() {
size(200, 500);
background(255);
Mover=new ClassNameMover();
}
void draw() {
Mover.applyForce(wind);
Mover.applyForce(gravity);
Mover.update();
Mover.display();
Mover.checkedge();
}
我这里图省事把你的class名称改成了 ClassNameMover,定义的object变量沿用了你之前的Mover; |