Hello everyone, I want to create fireworks, my idea is that when I click a firework is created that moves upwards, and once it exceeds 1/4 of the height of the screen it disappears and instead 10 is created sparks that separate in different directions, as you can see in my code I am working with an arrayList of fireworks, so it had occurred to me that the 10 sparks are created in a fixed arrangement of 10 positions, in which each position creates a spark, taking as position x and y the last position in which the firework was left … well, this is where I come with the development of this algorithm, I am stuck since I think it is a little more difficult than I imagined (or maybe not), how could I continue the code to achieve this? if you try it, the firework goes up but when it disappears the sparks are not seen on the screen (I guess because the update function is inside the for and they only appear for 1 iteration and then the background update “clears them” ) sorry for my english…
The main code:
ArrayList<Firework> fireworks = new ArrayList<Firework>();
Spark[] sparks = new Spark[10];
Firework f;
void setup(){
size(400, 400);
colorMode(HSB);
}
void draw(){
background(0);
for(int i=0; i < fireworks.size(); i++){
f = fireworks.get(i);
f.update();
f.move();
if (f.y < height/4){
for(int j=0; j < sparks.length; j++){
sparks[j] = new Spark();
sparks[j].update();
println(sparks[j].x, sparks[j].y);
}
fireworks.remove(i);
}
}
}
void mousePressed(){
fireworks.add(new Firework());
}
Firework class:
class Firework{
float x, y, velx, vely, diam=5;
Firework(){
this.x = random(0, width);
this.y = height-diam;
this.velx = random(-3, 3);
this.vely = random(5, 10);
}
void update(){
noStroke();
fill(255);
ellipse(x, y, diam, diam);
}
void move(){
x += velx;
y -= vely;
}
}
Spark class:
class Spark{
float x, y, velx, vely, diam=20;
Spark(){
this.x = f.x;
this.y = f.y;
this.velx = random(-3, 3);
this.vely = random(-3, 3);
}
void update(){
ellipse(x, y, diam, diam);
}
}