1
我在ArrayList/Particle系统上做了一个非常基础的教程。我一直得到一个“构造函数是未定义的错误”,我不明白为什么。谷歌搜索带来了很多更复杂的问题/答案。我错过了什么?去年这个变化了吗?构造函数没有定义[处理]
ArrayList<Particle> plist;
void setup(){
size(640, 360);
plist = new ArrayList<Particle>();
println(plist);
plist.add(new Particle());
}
void draw(){
background(255);
}
class Particle {
PVector location;
PVector velocity;
PVector acceleration;
float lifespan;
Particle(PVector l){
// For demonstration purposes we assign the Particle an initial velocity and constant acceleration.
acceleration = new PVector(0,0.05);
velocity = new PVector(random(-1,1),random(-2,0));
location = l.get();
lifespan = 255;
}
void run(){
update();
display();
}
void update(){
velocity.add(acceleration);
location.add(velocity);
lifespan -= 2.0;
}
void display(){
stroke(0, lifespan);
fill(175, lifespan);
ellipse(location.x, location.y,8,8);
}
boolean isDead(){
if(lifespan < 0.0){
return true;
}else{
return false;
}
}
}
啊,好的。构造函数和类对我来说都是新手,所以我就这样做了。错误消失了。我会回读构造函数。 –
@mishap_n说到参数,构造函数很像函数。如果您尝试在不带任何参数的情况下调用'ellipse()'函数,则会出现类似的错误。 –