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 › Issues with time
Page Index Toggle Pages: 1
Issues with time (Read 543 times)
Issues with time
Sep 17th, 2006, 5:25am
 
HI,

I am new to Processing, and I am trying to create a new rectangle with each second that pasts. So that I have one rect under another in a column, that keep getting added with each second.

here's my code:


void setup() {
 size(800,600);
}

void draw() {
 int x = 0;
 int s = second();  // Values from 0 - 59

 while(s < 59)
 {
   rect(5, x, 20, 20);
   x = x + 30;
 }


}


can anyone help>
Re: Issues with time
Reply #1 - Sep 17th, 2006, 3:29pm
 
You're on the right track, but you have a major problem in your code.  Let's say second() = 30.  Then when this code runs:

Code:

while(s < 59){
// Whatever here
}


You will be stuck in an infinite loop!  Since 30 is always less than 59, the program will never exit this loop.  Processing only updates the window at the end of draw() so the result will be a frozen, blank applet.

Since the draw() functions loops on its own, you don't need a while loop at all.  Instead, everytime through draw, check and see if the seconds have changed (using a "global" variable to keep track of what the seconds were the previous time through draw) and then draw your ellipse:

i.e.:

Code:

int s;

void draw() {
int newS = second();
if (newS != s) {
// do something
}
s = newS;
}

Re: Issues with time
Reply #2 - Sep 18th, 2006, 12:23pm
 
I guess this is exactly what you wanted? Smiley

int r=0,s=0,s2=0;
void setup()
{
size(800,600);  
}

void draw()
{
 s=second();
 if (s>s2 || (s2==59 && s==0))
{
 rect(5,r,20,20);
 r+=30;s2=s;
}
}
Page Index Toggle Pages: 1