You come up with the hard questions
In a word, not really (ok, two words). You can't assign the name of the variable dynamically, as they are not being evaluated at runtime. On the other hand, you CAN use something like HashMap to create a collection of variables that afford that type of flexibility. I've used that in a very special case where the graphic element behavior algorithm can be edited during runtime. It works surprisingly well...well, at least until I got stuck using embedded jython interpreter for advanced cases (man, is that thing slow!
I know, I can use javascript in Java6)...but I digress.
Probably none of the above is necessary in your case. Would the framework below work? It is pretty much a boilerplate.
What you may want to do is to create a Ball class, that looks like:
Code:
class Ball{
String name;
PVector location;
PVector velocity;
Ball(String name, PVector location, PVector velocity){
this.name = name;
this.location = location;
this.velocity = velocity;
}
void draw(){
//draw code
}
void update(){
//bouncy algorithm
}
}
...and in your main code,
Code:
ArrayList balls;
void setup(){
balls = new ArrayList();
//add balls like:
balls.add(new ball("ball_1", new PVector(10, 10), new PVector(10,10));
balls.add(new ball("ball_2", new PVector(20, 20), new PVector(10,-10));
}
void draw(){
//...some kind of routine
for(int i = 0; i < balls.size(); i++){
balls.get(i).draw();
balls.get(i).update();
}
}