ArrayList IndexOutOfBoundsException

Hi, not sure what I'm doing wrong here.

float x, y;
Boolean fire=false;
ArrayList<Missile> missiles;
ArrayList<ParticleSystem> ps;
Player p;
void setup() {
  size(800, 800);
  frameRate(60);
  p = new Player();
  missiles = new ArrayList<Missile>();
  ps = new ArrayList<ParticleSystem>();
}
void draw() {
  p.update();
  for (int i=missiles.size()-1;i>=0;i--) {
    if (missiles.get(i).isOutdated()) {
      ps.remove(i);
      missiles.remove(i);
    }
    else missiles.get(i).update();
  }
}
class Player {
  void update() {
    rectMode(LEFT);
    float dir = atan2(mouseY - height/2, mouseX - width/2);
    background(255);
    fill(0);
    pushMatrix();
    translate(width/2, height/2);
    rotate(dir+radians(270));
    rect(0, 0, 5, 24);
    popMatrix();
    rectMode(CENTER);
    rect(width/2, height/2, 15, 30);
    println(missiles.size());
    if (fire==true) {
      ps.add(new ParticleSystem());
      ;
      missiles.add(new Missile(width/2, height/2, dir, missiles.size()));
    }
    fire=false;
  }
}
class Missile {
  float x, y, dir, count;
  int id;
  Missile(float x, float y, float dir, int id) {
    this.id=id;
    this.x=x+cos(dir)*18;
    this.y=y+sin(dir)*18;
    this.dir=dir;
  }
  void update() {
    println(id);
    ps.get(id).run(new PVector(x, y));
    count+=1;
    x+=cos(dir)*2;
    y+=sin(dir)*2;
    pushMatrix();
    translate(x, y);
    strokeWeight(2);
    point(0, 0);
    popMatrix();
  }
  boolean isOutdated() {
    if (count>180) {
      return true;
    }
    return false;
  }
}
void mousePressed() {
  fire=true;
}
class Particle {
  PVector loc;
  PVector v;
  PVector acceleration;
  float lifespan;
  Particle(PVector l) {
    acceleration = new PVector(0, 0.05);
    v = new PVector(random(-1, 1), random(-2, 0));
    loc = l.get();
    lifespan = 99;
  }
  void run() {
    update();
    display();
  }
  void update() {
    v.add(acceleration);
    loc.add(v);
    lifespan -= 1;
  }
  void display() {
    noStroke();
    fill(255, 50, 50, lifespan*2);
    ellipse(loc.x, loc.y, 8, 8);
  }
  boolean isDead() {
    if (lifespan < 0.0) {
      return true;
    } 
    else {
      return false;
    }
  }
}
class ParticleSystem {
  ArrayList<Particle> particles;
  PVector origin;
  float count;
  ParticleSystem() {
    particles = new ArrayList<Particle>();
  }
  void run(PVector location) {
    count++;
    origin = location.get();
    if (count<120&&frameCount%8==0)particles.add(new Particle(origin));
    for (int i = particles.size()-1; i >= 0; i--) {
      Particle p = particles.get(i);
      p.run();
      if (p.isDead()) {
        particles.remove(i);
      }
    }
  }
}

Answers

  • which line is the error on?

  • ps.get(id).run(new PVector(x, y));

  • can't see anything specific (and can't run it here for now).

    but having two ArrayLists of things like that (missiles, ps) and hoping they stay in sync is asking for trouble really. you'd be better off adding a ps to the missile class, linking them more explicitly (assuming the ps dies when the missile does)

  • ah ok, thanks.

Sign In or Register to comment.