Looking for more resources about ArrayLists

here is a non-running part from a much bigger Sketch that shows

  • how to show bullets (it’s from a player who shoots) and
  • how to remove the bullets (in a backward for-loop) that are dead (because they left the screen or hit something, then the variable dead in the class Explosion is set to true). The core idea is to set a variable dead and don’t display the bullet when dead is true. Then in the backward for-loop we remove the dead ones finally from the ArrayList.

Chrisir

void ExplosionManager() {

  // this is a new for loop.
  // Actually for the Shrapnels.
  for (Explosion m : missiles) {
    m.decreaseLife(); // call the decrease life (for explosion)
    m.fly();             // call the "fly" method of the missile
    m.display();      // call the "display" method of the missile
  }
  //
  // remove dead ones (you need to have a conventional for-loop here, backwards)
  for (int i=missiles.size()-1; i>=0; i--) {
    Explosion m = (Explosion) missiles.get(i);
    if (m.dead) {
      missiles.remove(i);
    }
  }
} // func
//
1 Like