Automatic object generator - problem
in
Programming Questions
•
1 year ago
Dear forum
So i got this little code going on here and everything works right. but now i do want to put an automation in generating new objects, at where the program starts to fail, giving me the line:
ArrayIndexOutOfBoundsExeptions: 9
The program also highlights this line:
hallo[index++] = new Hello(geschwindigkeit, wahrfalsch);
Hello[] hallo;
boolean wahrfalsch;
float geschwindigkeit;
int anzahl = 9;
int index;
void setup()
{
size(500, 500);
frameRate(90);
background(255);
randomSeed(40);
hallo = new Hello[anzahl];
for(int b = 0; b <= anzahl; b++)
{
geschwindigkeit = random(8);
if (random(10) < 5) wahrfalsch = false;
hallo[index++] = new Hello(geschwindigkeit, wahrfalsch);
}
}
void draw()
{
for(int b = 1; b <= anzahl; b++)
{
hallo[b].farbenundpos();
hallo[b].fade();
hallo[b].zeigen();
}
}
class Hello
{
float r, g, b;
long l;
float k;
float x, y, x2, y2;
boolean toll = false;
boolean aufab;
Hello(float fadegesch, boolean updown)
{
k = fadegesch;
aufab = updown;
}
void farbenundpos()
{
if (toll == false){
r = random(255);
g = random(255);
b = random(255);
x = random(500);
y = random(500);
x2 = random(500);
y2 = random(500);
}
toll = true;
}
void fade()
{
l++;
if(l % 2 == 0) background(255);
if(aufab == false){
if(l % k == 0)
{
r--;
g--;
b--;
}
}
else if ( aufab == true)
{
if(l % k == 0){
r++;
g++;
b++;
}
}
if ((r + g + b) < 1) toll = false;
if ((r + g + b) > (3*255)) toll = false;
}
void zeigen()
{
stroke(r, g, b);
line(x, y, x2, y2);
}
}
Has anybody ever encounterd this problem?
thanks in advance
Sincerely
bluebubble
1