error : velocity(a vector) cannot be resolved or is not a field
error : velocity(a vector) cannot be resolved or is not a field
当我编译我的代码时,我收到一条错误消息,指出 velocity
(向量)无法解析或不是 field
。有没有人对可能导致此错误的原因有一些建议?
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
The below line is where the error occurs.
friction=b.velocity.get();
friction.normalize();
float c=-0.01;
friction.mult(c);
b.applyForce(friction);
b.bounce();
b.display();
}
这是球 class。
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);
}
}
感谢任何帮助。
问题是 velocity
不是 Ball
的 field
,它需要在 Ball
class 中才能工作。
尝试这样做:
class Ball {
PVector location;
PVector velocity;
PVector acceleration;
float mass, diam;
Ball() {
...
}
...
}
而不是:
PVector location;
PVector velocity;
PVector acceleration;
float mass, diam;
class Ball {
Ball() {
...
}
...
}
唯一的区别是我在 class 中包含了 5 个字段。
当我编译我的代码时,我收到一条错误消息,指出 velocity
(向量)无法解析或不是 field
。有没有人对可能导致此错误的原因有一些建议?
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
The below line is where the error occurs.
friction=b.velocity.get();
friction.normalize();
float c=-0.01;
friction.mult(c);
b.applyForce(friction);
b.bounce();
b.display();
}
这是球 class。
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);
}
}
感谢任何帮助。
问题是 velocity
不是 Ball
的 field
,它需要在 Ball
class 中才能工作。
尝试这样做:
class Ball {
PVector location;
PVector velocity;
PVector acceleration;
float mass, diam;
Ball() {
...
}
...
}
而不是:
PVector location;
PVector velocity;
PVector acceleration;
float mass, diam;
class Ball {
Ball() {
...
}
...
}
唯一的区别是我在 class 中包含了 5 个字段。