Trying to replicate a simple animation I did years ago in assembler. I want to draw a rectangle, display it for a set time, erase it, then draw another slightly larger rectangle, display for a time, erase, etc.
Thanks!
Here’s the code I’ve been using but it never draws the 1st rectangle.
void setup() {
size(640,360);
}
void draw() {
background(0); // create black background
fill(102); //set color to gray
rect(81, 81, 63, 63); // draw a rectangle
delay(3000); // wait 3 seconds
background(0); //Erase rectangle
fill(102); // gray again
rect(81, 81, 150, 150); // draw slightly larger rectangle
delay(3000); // wait 3 seconds
// Do it all over again
}
Don’t use delay() . It is not the function you want. It does not do what you think it does. It is not useful in this situation. Do not use delay() . Do not use delay() . Do not use delay() . Do not use delay() . Do not use delay() .
It’s the time that passed since the start of the sketch (not the current time) measured in milli seconds
It is very useful to make measurements like how much time has passed since the start of the sketch or to make a timer or like in this case switch between two states
When I was starting out processing, I wanted to make a timer without all of the millis() stuff(which I still don’t understand well enough to use) and I came up with an interesting example of a design. No idea if this would work implemented in a game or not.
Thanks for sharing this approach. Note that Processing already has a global frame count, frameCount. If you want to increase by n each frame, then you don’t need to store it in a global variable s if you don’t want to – the value is always equal to n*frameCount. So this is equivalent:
Yes, I understand that frameCount can be used more accurately, but, as far as I’m aware, you cannot change the value of frameCount at will. I made my timer with the intent of using it at different times, starting at possibly different values, such as a timer starting at the beginning of a level and it being reset per the next one.
Great! Under those circumstances, yes, it certainly does make sense to store the start time as a global variable.
You may still want to consider using that start time in a multiplication-based rather than additive calculation – if you aren’t changing rates during a single run and care about precision, of course. For variable rates, additive is the way to go – or fixing the past value every time the rate changes.