Processing: How to destroy an object once it leaves the screen? : In my code I have a class, `Key()` that can create a key at a random coordinate.  When a key is pressed, that key is added to an `ArrayList`. The `ArrayList` is iterated in the `draw()` method, and the key falls at a certain velocity. Multiple keys can be displayed at once. I want to remove a `Key` from the `ArrayList` once it leaves the view of the screen.

I have tried something like `if (key.location.y - textAscent() > height) {keys.remove(key)}` which either causes the program to stop working, or causes a letter to stop moving but remain in view once it reaches the bottom of the screen. Any suggestions?



    PFont f;
    ArrayList<Key> keys;

    void setup() {
      fullScreen(1);
      f=createFont("Bahnschrift", 300, true);
      textFont(f);
      keys = new ArrayList();
    }

    void draw() {
      fill(#FF5254);
      rect(0, 0, width, height);

      for (Key k : keys) {
        k.display();
        k.fall();
      }
    }


    void keyPressed() {
      keys.add(new Key());
    }

    class Key {
      PVector location, velocity;
      char k;
      color c;

      public Key() {
        this.c = 0;
        this.k=key;
        this.location = new PVector(random(width), random(height));
        this.velocity = new PVector(0, int(random(1, 11)));
      }

      void display() {
        fill(c);
        text(k, location.x, location.y);
      }

      void fall() {
        this.location.add(velocity);
      }
    }
