IndexOutOfBoundsException?
in
Programming Questions
•
2 years ago
Can someone help me out with this error? I highlighted it below. Whenever I press the play button this error comes up:
IndexOutOfBoundsException Index 20, Size: 20
PImage img;
ArrayList Bubbles;
void setup()
{
//Create the size of the sketch
size(600,400);
smooth();
Bubbles = new ArrayList();
for (int i=0; i < 20; i++)
{
Bubbles.add(new Bubble(random(0,width),random(0,height),random(0,5),random(0,5)));
}
// Load in image file
img = loadImage("gradient_blue.png");
}
void draw()
{
//Set the background
background(255);
// Display the image
image(img, 0, 0);
for(int i=0; 1 < Bubbles.size(); i++)
{
Bubble theBubble = (Bubble) Bubbles.get(i);
theBubble.update();
theBubble.display();
}
}
//New Tab - Class Bubble
class Bubble
{
PVector pos = new PVector();
PVector Spd = new PVector();
Bubble (float xPos, float yPos, float xSpd, float ySpd)
{
//Center ellipse
pos.x = xPos;
pos.y = yPos;
Spd.x = 1;
Spd.y = 2;
}
void update()
{
//Moving Ellipse
pos.add(Spd);
//Ellipse hits walls
if (pos.y +30 > height || pos.y -30 <0)
{
Spd.y *= -1;
}
if (pos.x +30 > width || pos.x -30 <0)
{
Spd.x *= -1;
}
}
void display()
{
//Create ellipse color
fill(22,205,240,60);
//Create ellipse stroke color
stroke(255);
//Create ellipse stroke weight (thickness)
strokeWeight(1);
//Create ellipse
ellipse(pos.x,pos.y,60,60);
}
}
1