Create multiple variables using for loop
in
Programming Questions
•
10 months ago
Hello.
I'm a noob, so please make answers as simple as possible. I want to create a program where there are balls moving randomly (done this bit). I've made the balls as a separate class, so I have to make a new variable every time I want to make a ball. This is fine for a couple of objects, but I want 20 balls (and at some point I will also want to vary this number).
I thought perhaps I could make a for loop that creates the variables? Something like...
for (i=1; i<21; i++) {
Ball ball[i];
}
void setup () {
for (i=1; i<21; i++) {
ball[i] = new Ball();
}
So this would make 20 balls, calling each one ball1, ball2, ball3 etc. How can I do this? If I cannot use a for loop, what other way would be good for making many variables like this?
Thank you!
1