We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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);
}
Answers
https://Processing.org/reference/ArrayList.html
http://docs.Oracle.com/javase/8/docs/api/java/util/List.html
http://studio.ProcessingTogether.com/sp/pad/export/ro.9jCT0UHalHCI3
http://studio.ProcessingTogether.com/sp/pad/export/ro.9oyKfI9kOIa77
Thanks for those, I think I am going to change the layout of my project a little bit. Reading and removing values from an array non-sequentially seems really tricky (if not impossible)