I converted this from a single instance of each object to implement 2 arrays of multiple ball objects + donut objects.
I get the error message the variable i does not exist.
Both arrays have been initialized in setup().
I tried commenting out one of the arrays to see if there was some kind of conflict but got the same message.
It’s probably a very simple oversight on my part but I can’t seem to locate the problem origin.
Any help is most appreciated.
Ball [] balls = new Ball [3];
Donut [] donuts = new Donut [3];
color backgroundColor = 255;
void setup() {
size(400, 400);
for (int i = 0; i < balls.length; i++) {
balls[i] = new Ball();
}
for (int i = 0; i < donuts.length; i++) {
donuts[i] = new Donut();
}
}
void draw() {
background(backgroundColor);
for (int i = 0; i < balls.length; i++);{
balls[i].display();
balls[i].move();
balls[i].bounce();
balls[i].intersect();
}
for (int i = 0; i < donuts.length; i++);{
donuts[i].display();
donuts[i].move();
donuts[i].bounce();
donuts[i].intersect();
}
}
//BALL CLASS ////////////////////////////////////////////////////////////////////
class Ball {
int x;
int y;
int r;
int speed;
boolean isBlue=false;
boolean firstTime=true; // for background
Ball() {
x = 0;
y = height/2;
r = 50;
speed = 5;
}
void display() {
//noFill();
if (isBlue){
fill(255, 0, 0, 50);
}
strokeWeight(3);
ellipse(x + r/2, y, r, r);
}
void move() {
x = x + speed;
}
void bounce() {
if ((x >= width - r)||(x <= 0)) {
speed = speed *-1;
}
}
void intersect() {
float distance = dist(x, y, donuts.x, donuts.y);
if (distance < r + donuts.r) {
if (firstTime){
backgroundColor = color(random(255));
}
isBlue=true;
firstTime=false;
} else {
firstTime=true;
}
}
}
//DONUT CLASS ////////////////////////////////////////////////////////////////////
class Donut {
int x;
float y;
int r;
float speed;
boolean isRed=false;
Donut() {
x = width/2;
y = 50;
r = 50;
speed = random(1, 5);
}
void display() {
noFill();
if (isRed){
fill(0, 0, 255, 50);
}
strokeWeight(3);
ellipse(x, y, r, r);
ellipse(x, y, r*2, r*2);
}
void move() {
y = y + speed;
}
void bounce() {
if ((y >= height - r)||(y <= 50)) {
speed = speed *-1;
}
}
void intersect() {
float distance = dist(x, y, balls.x, balls.y);
if (distance < r + balls.r) {
isRed=true;
}
}
}