Hello. I have this code I can’t solve.
I’m using two arraylists that draw balls in a random location. The first one, “Bola” (= “Ball” in Portuguese), is random colored and placed on canvas with a timer. The second one, “Blink”, is one that would be “blinking” (changing the alpha value) every few frames in the same location of Ball. However, only every few blinks are appearing, but either white or transparent, not switching on or off.
I know that I could just put <background(0)> in the draw() function, but then I’d have to draw every single Bola or Blink in the same place every frame, since I want them to stay in that same random place. I don’t know how to do that, especially with the timer. Also, I will be projecting this with a “transparent background” on a poster for a uni project, so even if I’d used background, I wouldn’t know how to project only the balls.
If anyone could be kind enough to help I would be very grateful! Thanks, Mariana
Main code:
ArrayList<Bola> bolas = new ArrayList<Bola>();
ArrayList<Blink> blinks = new ArrayList<Blink>();
// for the timer
int lastTime; // tempo que já passou
int duration = int (random(500, 2500));
// location of balls and blinks
float rX, rY;
float lifespan;
void setup () {
size(300, 300);
lifespan = 255.0;
// timer
lastTime = millis();
}
void draw() {
bolas.clear();
blinks.clear();
// bolas location
rX = random(width);
rY = random(height);
// timer
int elapsed = millis() - lastTime;
// pelo tempo total
if (elapsed > duration) {
lastTime = millis();
// adicionar bolas
Bola b = new Bola(rX, rY);
bolas.add(b);
}
for (int i = 0; i < bolas.size(); i++) {
Bola b = bolas.get(i);
b.display();
Blink v = new Blink(rX, rY);
blinks.add(v);
//if (v.areDead()) {
// blinks.remove(i-1);
//println("dead");
//}
}
for (Blink v : blinks) {
v.display();
}
}
The first class:
class Bola {
ArrayList<PVector> bolas;
float lx, ly;
// change hue
float h = random(0, 360);
Bola(float x, float y) {
lx = x;
ly = y;
bolas = new ArrayList<PVector>();
bolas.add(new PVector(x, y));
}
void display() {
noStroke();
fill(h, 90, 90);
ellipse(lx, ly, 30, 30);
println("ball displayed");
}
void add(float x, float y) {
bolas.add(new PVector(x, y));
}
}
The second class:
class Blink {
float lx, ly;
// to blink
boolean on = true;
ArrayList<PVector> blinks;
Blink(float x, float y) {
lx = x;
ly = y;
blinks = new ArrayList<PVector>();
blinks.add(new PVector(x, y));
}
void add(float x, float y) {
blinks.add(new PVector(x, y));
}
void display() {
noStroke();
//lifespan -= 1.0;
if (frameCount%5 == 0) {
if (on) {
fill(255, 255);
} else {
fill(255, 0);
on = !on;
}
}
ellipse(lx, ly, 25, 25);
println("add");
}
//is it still alive?
// boolean areDead() {
//if (lifespan < 0.0) {
//return true;
//} else {
//return false;
//}
//}
}
PS.: I was also attempting a lifespan to “kill” the Blink object but I need to solve that first! Thank you