Reputation: 1406
I am trying to do a very simple task where if a user clicks on the screen then background color should change 10 times randomly after every 200 ms.
Here is my code:
void setup()
{
size(400,400);
}
void draw()
{
}
void mousePressed()
{
for(int i = 0; i < 10; i++)
{
int startTime = millis();
background(int(random(255)), int(random(255)), int(random(255)));
while(millis() - startTime < 200){
}
}
}
However, the above code changes background color only once and not 10 times. I am not able to understand where I am going wrong.
Upvotes: 0
Views: 166
Reputation: 2854
Look at this article... http://wiki.processing.org/w/I_display_images_in_sequence_but_I_see_only_the_last_one._Why%3F The render in Processing is done only at the end of each draw cycle, so you only see the last color... I would try something like:
// to avoid starting at program's start...
boolean neverClicked = true;
// global
int startTime = 0;
// a counter
int times = 0;
void setup() {
size(400, 400);
}
void draw() {
// if clicked and passed half sec and 10 series not complete...
if ( !neverClicked && millis() - startTime > 500 && times < 9) {
// change bg
background(int(random(255)), int(random(255)), int(random(255)));
// reset clock
startTime = millis();
// count how many times
times++;
}
}
void mousePressed() {
neverClicked = false;
// reset timer and counter
startTime = millis();
times = 0;
// do first change immediately
background(int(random(255)), int(random(255)), int(random(255)));
}
Upvotes: 2