Loading...
Logo
Processing Forum

Create a delay?

in General Discussion  •  Other  •  1 year ago  

Hi, I wanted to create a delay in my game once for a second and searched for a function and found the Delay(); function but the wiki says:

delay() has been removed. Nobody understood what it did, and when they did, they didn't understand why it was there. Huge source of confusion, especially for beginning students.

 

So how can I reproduce the effect of this function?

 

Thanks in advance.

Replies(4)

Re: Create a delay?

1 year ago
You can do something similar to this... this is just a timer though that does something every X seconds.

Copy code
  1. long lastTime = 0;
  2. void setup() {
  3.   lastTime = millis();
  4. }
  5. void draw() {
  6.   if ( millis() - lastTime > 5000 ) {
  7.     println( "do things every 5 seconds" );
  8.     lastTime = millis();
  9.   }
  10. }

Delay was a bad thing because it halted the whole program, including the GUI - which means that buttons etc would stop to react and the user would think the program had crashed (or worse).

Re: Create a delay?

1 year ago
or you can make your own like this:

Copy code
  1. void setup()
  2. {
  3.   size(100,100);
  4. }
  5. boolean on = false;
  6. void draw()
  7. {
  8.   if(on) background(255,0,0);
  9.   else   background(0);
  10.   on = !on;
  11.   myDelay(6000);
  12. }
  13. void myDelay(int ms)
  14. {
  15.    try
  16.   {    
  17.     Thread.sleep(ms);
  18.   }
  19.   catch(Exception e){}
  20. }

Re: Create a delay?

19 days ago
delay()  is back since 2.0a5.

Re: Create a delay?

19 days ago
Yes, but it is still a bad idea to use it, because it halts your program which becomes unresponsive until delay() is finished.

(BTW, you answer to a one year old thread! Not sure of your point.)