Code
//from white to black, 2 second stall square
//variables
int clickcounter = 0;
int framecounter = 0;
int sqrColor = 255;
void setup (){
size (100,100);
}
void draw (){
background(sqrColor);
if ((clickcounter == 1) && (frameCount == (framecounter +120))){
sqrColor = 0;
}
if ((clickcounter == 2) && (frameCount == (framecounter +120))){
sqrColor =255;
}
}
void mousePressed(){
framecounter = frameCount;
clickcounter ++;
if (clickcounter > 2){
clickcounter = 1;
}
}
0303 - Maintaining state (simple timer): Storing and checking the frameCount
Statement:You may recall that the frameCount variable is an integer provided to you by the Processing environment, which reports the number of frames that your program has been running. (Each frame is approximately 1/60th of a second.) In this exercise, you will use the frameCount variable to help implement a simple timer.
Create a program which exhibits the following behavior:
- The canvas is white at the start of the program.
- At the exact moment that the user clicks on the canvas, store the current frameCount in one of your own global variables. You'll need this variable, because...
- When 120 frames have elapsed since the user clicked, set the background color to black. In other words, flip the color of the canvas (approximately) two seconds after the user clicks the mouse.
- If the user clicks the mouse again, wait another 120 frames and then set the canvas back to white. And so forth.