Shooting function on mouse event
in
Programming Questions
•
2 years ago
Hello everybody... :)
I'm learning to code so I'm sure my question must be really silly to answer for any of you.
I've made function called "rectangles" that randomly creates some rectangles on the canvas. I want these rectangles to be reloaded everytime I click the mouse or press a key (the example has been done with the mouse). I've also drawn a rectangle with the same color than the canvas which I located on the draw function with an opacity of 20 to make some sort of fade out effect. My problem is that everything works fine if I click only once, if I make further clicks nothing happens... any help? I've been struggling with this and I can't find a solution.
Many thanks!
float c = 0;
void setup() {
size(600, 600);
background(#B5D0D8);
noStroke();
smooth();
}
void draw() {
frameRate(10);
fill(#B5D0D8, 20);
rect(0, 0, width, height);
if (mousePressed == true) {
rectangles();
}
}
void rectangles() {
while (c < 600) {
fill(random(190), random(65), random(91));
rect(random(20, 540), random(20, 480), random(1, 120), random(1, 120));
rotate(0.01);
c = c + 1;
}
}
I'm learning to code so I'm sure my question must be really silly to answer for any of you.
I've made function called "rectangles" that randomly creates some rectangles on the canvas. I want these rectangles to be reloaded everytime I click the mouse or press a key (the example has been done with the mouse). I've also drawn a rectangle with the same color than the canvas which I located on the draw function with an opacity of 20 to make some sort of fade out effect. My problem is that everything works fine if I click only once, if I make further clicks nothing happens... any help? I've been struggling with this and I can't find a solution.
Many thanks!
float c = 0;
void setup() {
size(600, 600);
background(#B5D0D8);
noStroke();
smooth();
}
void draw() {
frameRate(10);
fill(#B5D0D8, 20);
rect(0, 0, width, height);
if (mousePressed == true) {
rectangles();
}
}
void rectangles() {
while (c < 600) {
fill(random(190), random(65), random(91));
rect(random(20, 540), random(20, 480), random(1, 120), random(1, 120));
rotate(0.01);
c = c + 1;
}
}
1