'How to draw multiple objects in succession one after another in Processing?
I would like to draw a single object for say 2 seconds, then draw the next object. I want to save each object so I can assess them after they each have run.
Object [] objects;
float starttime;
void setup() {
size (1600, 1000);
objects= new Object [100];
for (int i = 0; i < 100; i++) {
objects[i] = new Object();
}
}
void draw() {
background(255);
for (int i = 0; i < 100; i++) { //This makes multiple objects at once
while (2000 + starttime > millis()) { //This is my attempt at having them run one after the other
objects[i].makeobject();
}
}
}
Any help would be much apprecieated! Thank you
Solution 1:[1]
I suggest that you use a Timer. Processing has the millis() which tells you how long the sketch has been running. A long time ago I wrote a class to use as timer, here's the code for that class and also a simple demonstration with a rectangle moving with a 2 seconds delay.
class Delay {
int limit;
Delay (int l) {
limit = millis() + l;
}
boolean expired () {
return millis() > limit;
}
}
Delay timer;
int xpos = 0;
void setup() {
size (400, 100);
timer = new Delay(2000);
}
void draw() {
background(255);
if(timer.expired()) {
xpos += 100;
timer = new Delay(2000);
}
rect(xpos, 0, 99, 99);
}
Hope this helps. Have fun!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | laancelot |

