We are about to switch to a new forum software. Until then we have removed the registration on this forum.
int max = 1000;
Clover[] cloversub = new Clover[max];
Greenleaf[] greenleafsub = new Greenleaf[max];
Redleaf[] redleafsub = new Redleaf[max];
PImage cloverimg, greenleafimg, redleafimg;
int totalLeaves = 0;
void setup(){
size(400,400);
}
void draw(){
background(200);
for (int i = 0; i < totalLeaves; i++){
cloversub[i].speed();
cloversub[i].display();
}
for (int i = 0; i < totalLeaves; i++){
greenleafsub[i].speed();
greenleafsub[i].display();
}
for (int i = 0; i < totalLeaves; i++){
redleafsub[i].speed();
redleafsub[i].display();
}
if (totalLeaves >= max){
totalLeaves = 0;
}
}
void keyPressed(){
if (totalLeaves<max && key == '1'){
cloversub[totalLeaves] = new Clover();
totalLeaves++;
}
if (totalLeaves<max && key == '2'){
greenleafsub[totalLeaves] = new Greenleaf();
totalLeaves++;
}
if (totalLeaves<max && key == '3'){
redleafsub[totalLeaves] = new Redleaf();
totalLeaves++;
}
}
The error appears every time I execute the code and press 1, 2 or 3. When I press 2 or 3, the error appears at cloversub[i].speed();. If I press 1, the error appears at greenleafsub[i].speed();.
When I only use one subclass it works just fine. It start giving errors when I try to use multple subclasses.
I am new to processing and I have no idea how to fix this. Any help please?
Answers
At the beginning you have empty arrays. That's not a problem, because totalLeaves is zero. But when you press a key, you create an element in one of the arrays but the others are still empty.
You should maybe use seperate variables to keep track of the number of clovers, greenleafs and redleafs. Increment only the variable that is related to the array where you created a new element and then use these variables to iterate over the arrays in draw().
Thank you so much! It works!