U can create a class for that:
something like this:
class Ball
{
float x, y, size;
Ball ( float _x, float _y, float _sz)
{
x = _x;
y = _y;
size = _sz
}
void display()
{
ellipse(x, y, size,size);
}
void move()
{
//code for moving
}
boolean finished()
{
// test for out of window
return false; // left this as a placeholder, so balls are never finished ... never removed ...
}
}
This won't move nothing but should already compile... (untested)
What seemed strange for me first time i saw an ArrayList is this creating an object for each iteration and the casting stuff. in this part (see comments):
for (int i = balls.size()-1; i > 0; i--) {
Ball ball = (Ball) balls.get(i); // for each iteration creates a temp Ball object
// and assign to it the Ball in the array at i
// ball (temp obj) = balls.get(i); (the Ball in your ArrayList)
// the (Ball) is the casting.see note below
ball.move(); // cal stuff using temp obj
ball.display();
if (ball.finished()) {
// Items can be deleted with remove().
balls.remove(i);
}
// here this temp is "discarded"
// and another one will be created for the next iteration
}
I think this example is old from when Processing could not use generics, (<type>) if u do like clankil3r sad
ArrayList<Ball> balls = new ArrayList<Ball>();
U don't need to cast as you told that this array is holding Balls, so:
Ball ball = balls.get(i);
should work and, i think, not sure, even the more like array syntax also:
balls.get(i).move();
Works?