我有下面的程序闪烁红色和白色。有可能每10秒有一个时间间隔,红色保持稳定几秒钟,然后再次闪烁。
float delay = 1;
void setup() {
size(200, 200);
}
void draw() {
background(96);
fill(255);
if(frameCount%(2*delay)<delay) fill(255, 0, 0);
rect(50, 50, 100, 100);
}
一个应用于代码中的简单计时器的简单示例:
int delay = 1000;// ONE SEC
int now;
//a flag
boolean red = false;
void setup() {
size(200, 200);
//start the counter
now = millis();
}
void draw() {
background(96);
if (millis() - now > delay) {
//change flag
red = !red;
//reset counter
now = millis();
}
if (red)
fill(255, 0, 0);
else
fill(255);
rect(50, 50, 100, 100);
}