Deleting object in array using a conditional?

edited December 2015 in Questions about Code

Hi, I am wondering if it is possible to delete objects in an array using an if statement? In my project I have a simple function that creates an image of a tree when you click. The trees are stored in an array with the mouseX and mouseY coordinates. There is another bulldozer image moving across the screen.

Is it possible to delete one of the tree images in the array when the bulldozer image intersects with it?

    PImage ground;
    PImage bull;
    PImage bull2;
    PImage bull3;
    PImage bull4;
    PImage bull5;
    PImage tree;
    float a;
    float b;
    float c;
    float d;
    float e;
    float speed = 2;

    Tree[] trees = new Tree[0];

    void setup () {
      size (600,500);
      ground = loadImage("ground.jpg");
      bull = loadImage("bulldoze.png");
      bull2 = loadImage("bulldoze2.png");
      bull3 = loadImage("bulldoze.png");
      bull4 = loadImage("bulldoze2.png");
      bull5 = loadImage("bulldoze.png");
      tree = loadImage("tree.png");
    }

    void draw () {
      background (255);
      image(ground, 0, 0);
      image(bull, a, 0);
      if (a < width+20) {a += speed;}

      if (a > width) {
      image(bull2, 620-b, 100);
      if (b > -20) {b += speed;}
      }

      if (b > width) {
      image(bull3, c-300, 200);
      if (c < width+320) {c += speed;}
      }

      if (c > width+300) {
      image(bull2, 620-d, 300);
      if (d > -20) {d += speed;}
      }

      if (d > width) {
      image(bull3, e-300, 400);
      if (e < width+170) {e += speed;}
      }

      for(int i=0; i<trees.length; i++){
        trees[i].display();
      }

    }  
      void mousePressed() {
        Tree b = new Tree(mouseX, mouseY);
        trees = (Tree[])append(trees, b);
      }
Tagged:

Answers

Sign In or Register to comment.