We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi. Im using the following code, and I want my shapes to fadeout after an amount of time. But they flicker before they fade out completely. Does anyone know why this is happening?
ArrayList<Balls> balls = new ArrayList<Balls>();
void setup() {
size(400, 400);
}
void draw() {
background(0);
for (int i=0; i<balls.size(); i++) {
balls.get(i).setTimer();
balls.get(i).drawBalls();
if (balls.get(i).timerCheck()) {
balls.remove(i);
}
}
}
void mouseDragged() {
balls.add(new Balls(mouseX, mouseY));
}
class Balls {
float x, y;
color colors, colorsStroke;
int timer;
float fadeOut = 1.;
float randomLife = random(80, 200);
Balls(int mousex, int mousey) {
colors = color(random(255), random(255), random(255));
colorsStroke = color(random(255), random(255), random(255));
rectMode(CENTER);
x = mousex;
y = mousey;
}
void setTimer() {
timer++;
}
boolean timerCheck() {
boolean check = false;
if (timer>randomLife) {
fadeOut = fadeOut - 0.01;
}
if (fadeOut<=0.) {
fadeOut = 0.;
check = true;
}
return check;
}
void drawBalls() {
strokeWeight(2);
stroke(colorsStroke, 255*fadeOut);
fill(colors, 50*fadeOut);
pushMatrix();
translate(x, y);
ellipse(0, 0, 30, 30);
popMatrix();
}
}
Answers
for ( ; ; )
loop!for ( ; ; )
loop instead.for (int i = balls.size(); i-- != 0; ) { }
Ball b = get(i);