If you want to make a ball at some random position I would do something like the below.
ArrayList<Ball> balls = new ArrayList<Ball>();
//Option 1: return a ball to be added like balls.add(makeBall());
private Ball makeBall()
{
Random rand = new Random();
Ball newBall = new Ball();
newBall.x = rand.nextFloat() * FRUSTUM_WIDTH;
newBall.y = rand.nextFloat() * FRUSTUM_HEIGHT;
newBall.radius = rand.nextFloat() * MAX_RADIUS;
return newBall;
}
//Option 2: add the ball to the list inside the method
private void newBall()
{
Random rand = new Random();
balls.add(new Ball(rand.nextFloat() * FRUSTUM_WIDTH, rand.nextFloat() * FRUSTUM_HEIGHT,
rand.nextFloat() * MAX_RADIUS));
}
This would have a ball class which stores an x and y position and a radius. The list will store all the balls and be looped through to update and render the balls. Option 1 has a blank constructor and the fields (x, y, radius) must be initialized manually. Option 2 has a constructor public Ball(float xPosition, float yPosition, float radius).
Edit: Then you would call this
for(int i = 0; i < balls.size(); i++)
{
float x = balls.get(i).getX();
float y = balls.get(i).getY();
//check collisions here
}
Edited by David.M, 12 August 2012 - 04:08 PM.