2014-01-17 2 views
1

프로젝트의 몰려들 시뮬레이션을 수정하고 있습니다. 객체 추가는 괜찮지 만 객체를 제거하려고하면 "함수 크기()가 존재하지 않습니다"라는 오류가 240 행에 발생합니다.이 문제는 AdjSize() 및 subBoid()에서 끝나지만 아마도 알지 못합니다 그게 원인이야. 코드를 단순화하여 ArrayList를 모으는 것입니다.ArrayList에 "size()가 없습니다."오류가 발생했습니다.

Flock flock; 
float k, l; 
int previous = 0; 
int test = 0; 

void setup() 
{ 
    size(1920, 1080); 
    flock = new Flock(); 
    for (int i = 0; i < 150; i++) 
    { 
    flock.addBoid(new Boid(width/2,height/2)); 
    } 
} 

void draw() 
{ 
    background(0); 
    flock.run(); 
    flock.AdjSize(); 
} 



// The Boid class 
class Boid 
{ 

    PVector location; 
    PVector velocity; 
    PVector acceleration; 
    float r; 
    float maxforce; // Maximum steering force 
    float maxspeed; // Maximum speed 

    Boid(float x, float y) 
    { 
    acceleration = new PVector(0, 0); 

    float angle = random(TWO_PI); 
    velocity = new PVector(cos(angle), sin(angle)); 

    location = new PVector(x, y); 
    r = 2.0; 
    maxspeed = 2; 
    maxforce = 0.03; 
    } 



    void run(ArrayList<Boid> boids) 
    { 
    flock(boids); 
    update(); 
    borders(); 
    render(); 
    } 

    void applyForce(PVector force) 
    { 
    acceleration.add(force); 
    } 

    void flock(ArrayList<Boid> boids) 
    { 
    PVector sep = separate(boids); // Separation 
    PVector ali = align(boids); // Alignment 
    PVector coh = cohesion(boids); // Cohesion 
    sep.mult(1.5); 
    ali.mult(1.0); 
    coh.mult(1.0); 
    applyForce(sep); 
    applyForce(ali); 
    applyForce(coh); 
    } 

    // Method to update location 
    void update() 
    { 
    // Update velocity 
    velocity.add(acceleration); 
    // Limit speed 
    velocity.limit(maxspeed); 
    location.add(velocity); 
    // Reset accelertion to 0 each cycle 
    acceleration.mult(0); 
    } 

    PVector seek(PVector target) 
    { 
    PVector desired = PVector.sub(target, location); 
    desired.normalize(); 
    desired.mult(maxspeed); 

    PVector steer = PVector.sub(desired, velocity); 
    steer.limit(maxforce); // Limit to maximum steering force 
    return steer; 
    } 

    void render() 
    { 
    float theta = velocity.heading2D() + radians(90); 

    fill(255, 255,255); 
    stroke(255); 
    pushMatrix(); 
    translate(location.x, location.y); 
    rotate(theta); 
    beginShape(); 
    vertex(r*15, r*17); //Left wing tip 
    vertex(r*25, r*10); //Left wing top point 
    vertex(r*30, r*13); //middle divit 
    vertex(r*35, r*10); //right wing top point 
    vertex(r*45, r*17); //right wing tip 
    vertex(r*30, r*13); //underswoop 
    endShape(CLOSE); 
    popMatrix(); 
    } 

    void borders() 
    { 
    if (location.x < -r) location.x = width+r; 
    if (location.y < -r) location.y = height+r; 
    if (location.x > width+r) location.x = -r; 
    if (location.y > height+r) location.y = -r; 
    } 

    PVector separate (ArrayList<Boid> boids) { 
    float desiredseparation = 25.0f; 
    PVector steer = new PVector(0, 0, 0); 
    int count = 0; 
    for (Boid other : boids) 
    { 
    float d = PVector.dist(location, other.location); 
    if ((d > 0) && (d < desiredseparation)) 
    { 
     PVector diff = PVector.sub(location, other.location); 
     diff.normalize(); 
     diff.div(d);  // Weight by distance 
     steer.add(diff); 
     count++;   // Keep track of how many 
    } 
    } 
    if (count > 0) 
    { 
    steer.div((float)count); 
    } 
    if (steer.mag() > 0) 
    { 
    steer.normalize(); 
    steer.mult(maxspeed); 
    steer.sub(velocity); 
    steer.limit(maxforce); 
    } 
    return steer; 
    } 

    // Alignment 
    PVector align (ArrayList<Boid> boids) 
    { 
    float neighbordist = 50; 
    PVector sum = new PVector(0, 0); 
    int count = 0; 
    for (Boid other : boids) 
    { 
     float d = PVector.dist(location, other.location); 
     if ((d > 0) && (d < neighbordist)) 
     { 
     sum.add(other.velocity); 
     count++; 
     } 
    } 
    if (count > 0) 
    { 
     sum.div((float)count); 
     sum.normalize(); 
     sum.mult(maxspeed); 
     PVector steer = PVector.sub(sum, velocity); 
     steer.limit(maxforce); 
     return steer; 
    } 
    else 
    { 
     return new PVector(0, 0); 
    } 
    } 

    PVector cohesion (ArrayList<Boid> boids) { 
    float neighbordist = 50; 
    PVector sum = new PVector(0, 0); 
    int count = 0; 
    for (Boid other : boids) 
    { 
    float d = PVector.dist(location, other.location); 
    if ((d > 0) && (d < neighbordist)) 
    { 
     sum.add(other.location); // Add location 
     count++; 
    } 
    } 
    if (count > 0) 
    { 
     sum.div(count); 
    return seek(sum); // Steer towards the location 
    } 


    else 
    { 
     return new PVector(0, 0); 
    } 
} 
} 
class Flock 
{ 
    ArrayList<Boid> boids; // An ArrayList for all the boids 

    Flock() 
    { 
    boids = new ArrayList<Boid>(); // Initialize the ArrayList 
    } 

    void run() 
    { 
    for (Boid b : boids) 
    { 
     b.run(boids); 
    } 
    } 

    void AdjSize() 
    { 
    if(test == 0) 
    { 
     flock.addBoid(new Boid(width/2,height/2)); 
     test = 1; 
    } 
    else 
    { 
    flock.subBoid(flock.size() - 1); 
    } 
    } 

    void addBoid(Boid b) 
    { 
    boids.add(b); 
    } 

    void subBoid(Boid b) 
    { 
    boids.remove(b); 
    } 
} 
+1

오류 메시지를 다시 살펴보십시오. 문제가 무엇인지 정확하게 알려줍니다. – EJP

답변

4

당신은하지 ArrayList에에, 당신의 무리 객체에 size()를 호출하고 있습니다. Flock이 호출 가능한 size() 메소드를 갖기를 원한다면, 먼저 이것을 주어야합니다. 예 :

class Flock { 
    // .... 

    public int size() { 
    return boids.size(); 
    } 
}