Using millis() and random() together...
in
Programming Questions
•
1 year ago
Huh. Seems like that sweet answer disappeared. Anyway, if you want to display a loading percentage that goes up 5% at a time, and takes between 5 and 10 seconds, you can do this:
- int totalTime;
- PFont myFont = createFont( "Ariel", 12 );
- void setup() {
- size(200,200);
- fill(0);
- totalTime = 5 + int(random(6));
- textFont( myFont );
- }
- void draw() {
- background(255);
- text( str(totalTime) + " seconds", 20, 20 );
- text( str(5 * int(map(constrain(millis(),0,1000*totalTime), 0, 1000 * totalTime, 0, 20)) ) + "%", width/2-15, height/2+5);
- }
1