I’m making a basic “Life” game, where larger circles eat smaller ones, and I keep getting a seemingly random IndexOutOfBoundsException.
This is the section of code that causes the error:
for (int i = beings.size() - 1; i >=0; i--) {
if (i < beings.size() ) {
beings.get(i).display();
beings.get(i).move();
for (int j = beings.size() - 1; j >=0; j--) {
if (j < beings.size() ) {
if (beings.get(i).inside(beings.get(j).x, beings.get(j).y, beings.get(j).s) && beings.get(i).s > beings.get(j).s ) {
beings.get(i).r = (beings.get(i).r + beings.get(j).r)/2;
beings.get(i).g = (beings.get(i).g + beings.get(j).g)/2;
beings.get(i).b = (beings.get(i).b + beings.get(j).b)/2;
beings.remove(j);
}
}
};
if (beings.get(i).x > width || beings.get(i).x < 0 || beings.get(i).y > height || beings.get(i).y < 0) {
beings.remove(i);
}
}
}
The highlighted line is the if (beings.get(i).inside(beings.get(j).x, beings.get(j).y, beings.get(j).s) && beings.get(i).s > beings.get(j).s ) {. I tried adding safeguards to the code and searching the internet, but I couldn’t find anything.
And in case it’s helpful, here’s the being.inside function:
boolean inside(int x2, int y2, int s2) {
int r2 = s2/2;
return (x - x2) * (x - x2) + (y - y2) * (y - y2) <= r2 * r2;
}
Thanks in advance.