( back ) Creating
a simple timer
This sketch shows how to construct a countdown timer
effect.
Animation timer;
BFont font;
long duration = 30*1000; // 30 seconds.
void setup()
{
size(200,100);
strokeWeight(1);
font = loadFont("RotisSanSer.vlw.gz");
textFont(font, 20);
timer = new Animation(this, loadImage("timer.gif"), 65,40, 75,0);
timer.setDuration(duration);
}
void loop()
{
background(127);
rect(65,39, 75,timer.imageHeight());
timer.draw();
text("Time remaining: "
+ (duration - (int)(timer.currentPercentage() * duration)) / 1000
+ " seconds.", 15, 25);
}
|