We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I want to make a program to show an object (Macabeu) 10 times in random position and, when the user clicks on one of the objects, it desappears. I have made this code:
ArrayList<Macabeu> macabeus ;
int[] X =new int[10];
int[] Y =new int[10];
void setup() {
size(450, 450);
macabeus= new ArrayList<Macabeu>();
for (int i=0; i<10; i++) {
macabeus.add(new Macabeu());
}
}
void draw() {
background(0, 0, 0);
for (int i=0; i<10; i++) {
Macabeu tmp = macabeus.get(i);
X[i]=tmp.x;
Y[i]=tmp.y;
println(X[i], ",", Y[i]);
tmp.pintar();
}
}
void mouseClicked() {
for (int i =0; i<macabeus.size(); i++) {
if (dist(mouseX, mouseY, X[i], Y[i])<30)
{
macabeus.get(i);
macabeus.remove(i);
}
}
}
And I made this class in a new tab:
class Macabeu {
PImage mac;
int x;
int y;
Macabeu() {
x=int(random(0, 400));
y=int(random(0, 400));
}
void actualizar() {
}
void pintar() {
mac = loadImage("macabeu.png");
imageMode(CENTER);
image(mac, x, y, 60, 60);
}
}
It doesn't work and an error message appears: "IndexOutOfBoundsException: Index:9, Size:9" and marks the line 19 which is " Macabeu tmp= macabeus.get(i); "
I don't have idea what to do :( Can anybody help me with this?
Answers
https://forum.Processing.org/two/discussions/tagged/backwards
variables traditionally start with a lower case letter. so x rather than X.
you have nine elements, numbered 0 to 8. you are trying to access item 9, which doesn't exist.
you've probably removed an element (line 31) but are still using 10 in the for loop on line 15. change the 10 to macabeus.size().
:D =D> It works!!! Thank you so much koogs!!