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 & HelpPrograms › problem with delay
Page Index Toggle Pages: 1
problem with delay (Read 1048 times)
problem with delay
May 9th, 2005, 9:07am
 
This may be a question for the syntax forum but from what I can tell in the reference section, I'm using the framerate/delay calls correctly.  Here's the program:

float initx, inity, initangle, hlength, sangle, trotation, val;
boolean lowval;

void setup(){
 size(900,900);
 background(70);
 strokeWeight(15);
 framerate(69);
}
void mouseReleased(){
 initx = mouseX;
 inity = mouseY;
 initangle = PI/3;
 hlength = 200;
 sangle = PI/24;
 trotation = 4*PI;
 val = 0;
 lowval = true;
}
void draw(){
 for(float i=initangle; hlength>0; i+=sangle){
   stroke(val);
   line(initx, inity, sin(i)*hlength+initx, cos(i)*hlength+inity);
   hlength = hlength-0.8;
   if(val==256){lowval = !lowval;}
   if(lowval){val+=2;}
     else if(!lowval){val-=2;}
   if(val==0){lowval = true;}
   delay(250);
 }
}

The goal is for a spiral to be drawn where you click the mouse.  If I take the delay call out of the code it draws the spiral, but I want to use delay to make it so you can see the bars in the spiral being drawn.  That is, I want a slight pause after every line is drawn.  But when I run this code with the delay call nothing is drawn.  Am I using delay incorrectly? Maybe I'm not supposed to put it in a loop? If that's the case how can I do what I'm looking for here?
Re: problem with delay
Reply #1 - May 9th, 2005, 1:45pm
 
The screen is updated every time draw() is finished and only when draw() is finished, looped or not. If you have something in a for loop in draw(), every iteration gets drawn before the draw() is ended and result is displayed on screen.

Therefor, in animation, you have to control the iteration for every loop of the draw(), and in your case also have a switch checked. For example like this:

Code:

float initx, inity, initangle, hlength, sangle, trotation, val;
boolean lowval;

boolean mouseSwitch=false; // This is a new one
float i; // The for loop i variable is now global

void setup(){
size(600,600);
background(70);
strokeWeight(15);
framerate(69);
}

void mouseReleased(){
initx = mouseX;
inity = mouseY;
initangle = PI/3;
hlength = 200;
sangle = PI/24;
trotation = 4*PI;
val = 0;
lowval = true;

mouseSwitch=true; // Set switch if mouse is clicked
i=initangle; // Same as in your for loop
}

void draw(){
if(mouseSwitch){ // If clicky, lets do something
i+=sangle; // Same as in your for loop

stroke(val);
hlength = hlength-0.8;
if(val==256){ lowval = !lowval; }
if(lowval){ val+=2; }
else if(!lowval){ val-=2; }
if(val==0){ lowval = true; }

// Moved down to be after calculations
line(initx, inity, sin(i)*hlength+initx, cos(i)*hlength+inity);

// Same as in your for loop, switch off when finished the spiral
if(hlength<=0) mouseSwitch=false;

delay(250);
}
}
Re: problem with delay
Reply #2 - May 9th, 2005, 10:21pm
 
Thanks! Problem solved.
Page Index Toggle Pages: 1