2016-09-15 173 views
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; 
    } 
    } 
} 

回答

2

这是您的Particle构造:

Particle(PVector l){ 

注意,它需要一个PVector说法。

这是你如何调用Particle构造:

plist.add(new Particle()); 

此行有一个错误:the constructor粒子()does not exist.这就是你的问题是什么。构造函数Particle()不存在。只有Particle(PVector)存在。

换句话说,请注意您没有给它一个PVector参数。这就是你的错误告诉你的。

要解决这个问题,您需要提供一个PVector参数,或者您需要更改构造函数以使其不再需要。

+0

啊,好的。构造函数和类对我来说都是新手,所以我就这样做了。错误消失了。我会回读构造函数。 –

+0

@mishap_n说到参数,构造函数很像函数。如果您尝试在不带任何参数的情况下调用'ellipse()'函数,则会出现类似的错误。 –

相关问题