IndexOutOfBoundsException
in
Programming Questions
•
10 months ago
I'm getting this error on a game I am designing for my ArrayList debris (the highlighted line is underlined and made bold in the code below). Specifically, the error says IndexOutOfBoundsException: index 2, Size: 2. I'm having trouble spotting the problem, perhaps someone else can help me? The code is below (the relevant code from what I can gather):
If you think more code is needed to resolve this issue just say so.
ArrayList debris;
void setup() {
size(800,800);
smooth();
background(255);
//Initializing all the elements of the array
for (int i = 0; i < comets.length; i++) {
comets[i] = new Comet();
}
score = 0;
img = loadImage("space.jpg");
debris = new ArrayList();
tp = 2;
}
void draw()
{
//Creating random Debris
float randomNumber = random(0, 15);
if((int)randomNumber == 1)
{
debris.add(new Debris(tp));
}
for(int i = 0; i < debris.size(); i++)
{
Debris d = (Debris) debris.get(i);
d.update();
d.display();
//Debris collision
for(int c = 0; c < comets.length; c++)
{
if(dist(comets[c].location.x, comets[c].location.y, d.location.x, d.location.y) <= 20)
{
//println("Debris Destroyed");
debris.remove(i);
score = score + 25;
}
}
//Game over method
if(dist(d.location.x, d.location.y, width/2, height/2) <= 20)
{
println("Game Over");
println("Final Score: " + (score + frameCount));
//println("Game Reset");
//setup();
noLoop();
}
//Increase speed as game goes on
if(frameCount % 500 == 0)
{
d.topspeed = d.topspeed + 1;
tp = (int)d.topspeed;
//println(tp);
}
}
If you think more code is needed to resolve this issue just say so.
ArrayList debris;
void setup() {
size(800,800);
smooth();
background(255);
//Initializing all the elements of the array
for (int i = 0; i < comets.length; i++) {
comets[i] = new Comet();
}
score = 0;
img = loadImage("space.jpg");
debris = new ArrayList();
tp = 2;
}
void draw()
{
//Creating random Debris
float randomNumber = random(0, 15);
if((int)randomNumber == 1)
{
debris.add(new Debris(tp));
}
for(int i = 0; i < debris.size(); i++)
{
Debris d = (Debris) debris.get(i);
d.update();
d.display();
//Debris collision
for(int c = 0; c < comets.length; c++)
{
if(dist(comets[c].location.x, comets[c].location.y, d.location.x, d.location.y) <= 20)
{
//println("Debris Destroyed");
debris.remove(i);
score = score + 25;
}
}
//Game over method
if(dist(d.location.x, d.location.y, width/2, height/2) <= 20)
{
println("Game Over");
println("Final Score: " + (score + frameCount));
//println("Game Reset");
//setup();
noLoop();
}
//Increase speed as game goes on
if(frameCount % 500 == 0)
{
d.topspeed = d.topspeed + 1;
tp = (int)d.topspeed;
//println(tp);
}
}
1