We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › delay() used with noLoop/keyPressed->redraw
Page Index Toggle Pages: 1
delay() used with noLoop/keyPressed->redraw (Read 625 times)
delay() used with noLoop/keyPressed->redraw
Nov 28th, 2006, 11:05am
 
Hi,

I am trying to write a program meant to carry out a formal visual experiment. The idea is to show a picture for a fixed duration, let's say 1s, then the background should remain white until a key is pressed. At this point the picture is shown again for 1s with som changes), and so on... To do that I thought to use the functions noLoop(), and redraw() inserted in the function keyPressed() to rerun the program. Inside the draw() function I  also use the function delay(1000) to show the picture for 1s. However this does not work and I don't understand why. I get the picture once for 1s, but then when I press a key nothing happens anymore.

Thanks in advance for your help!

Delphine.
Re: delay() used with noLoop/keyPressed->redraw
Reply #1 - Nov 29th, 2006, 12:20am
 
Hi Delphine,

delay() has given me a lot of problems in the past, so I try to avoid it if possible. The reason why your function doesn't work as intended is probably related to calling the delay within draw() function, which is called about 40-60 times a second. Delaying 1 second every run isn't exactly what you want to do in this case.

The better solution would be to have a conditional within your draw function which checks time elapsed on every call--something like this, roughly:

Code:

PImage picture;
boolean keyNotPressedYet = true;
float timeToShowPicture = ...;

void draw()
{
//calculate time since setting up picture using millis() or something like that
float elapsed = ...;
if(elapsed <= timeToShowPicture)
{ image(picture, etc. etc...);}
else
{
if(keyNotPressedYet)
{ //draw white}
else
{//setup for new image, which should reset keyNotPressedYet}
}
}

void keyPressed()
{
keyNotPressedYet = false;
}


From here, just create a function that resets the start time, or however you are going to calcualte elapsed time, and change the image that would be displayed. My suggestion is to leave that image as a global variable, and have that redrawing/re-setup function to change that image.

I hope this gives you some ideas on how to solve your problem!
Re: delay() used with noLoop/keyPressed->redraw
Reply #2 - Nov 29th, 2006, 11:43am
 
Thank you for your reply. I tried to follow your idea, but I was not sure about how to use millis() which gives an absolute elapsed time... But I found another way which works fine by using loop() and noLoop() :

Code:

void draw()
{
 // draw picture;
 i++;
 
 if (i>= frameRate){
   background(bg);
   noLoop();  
  }
}

void keyPressed() {
 loop();
 i = 0;
 //new settings for the picture;
}
Re: delay() used with noLoop/keyPressed->redraw
Reply #3 - Nov 29th, 2006, 12:11pm
 
I'm sorry that you couldn't make use of my example. You can find out how millis() works by taking a look at the API reference page at http://processing.org/reference/millis_.html

However, looking at the code you posted, I have trouble seeing how that solves your original goal. Even working exactly how you intend it to, the line:

Code:

if (i >= frameRate)


seems either prone to acting inconsistently across different operating platforms, and perhaps unable to scale up should you wish to change the duration of displaying the image.

Still, if your code works, then congratulations Smiley
Page Index Toggle Pages: 1