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 › why my code doesn't stay in loop
Page Index Toggle Pages: 1
why my code doesn't stay in loop? (Read 565 times)
why my code doesn't stay in loop?
May 30th, 2009, 6:16pm
 
Hi,

In this simple code I expect that the rectangle's colour keeps changing indefinetly. For some reason that's not happening. It changes the colour once and get's freezed on that one.

Code:
void setup() {  
size(200,200);
rectMode(CENTER);
}



void draw() {
background(0);
fill(255,0,0);
rect(100,100,50,50);
delay(200);
background(0);
fill(0,0,255);
rect(100,100,50,50);
delay(200);
}


I can`t find the reason why the code doesn't keep looping inside "draw()".

I need to figure this out because I'm having problems with a code that has to display different images depending on the information recieved from the serial port. I realized that after loading and displaying an image, the screen does not refresh (the next image is not displayed). I'm sure it's a display problem because the event that triggers the image change is working fine.

I hope that someone can help me with this.

Thanks!

Martin
Re: why my code doesn't stay in loop?
Reply #1 - May 30th, 2009, 7:06pm
 
I'm not sure why either, but try this: it should do what you wanted to do.

(sorry, I'm not sure how to use this new editor.)


boolean toggle;
void setup() {  
size(200,200);
rectMode(CENTER);
toggle = true;
}


void draw() {  
 background(0);  
 if(toggle)
   fill(255,0,0);
 else
   fill(0,0,255);
 
 rect(100,100,50,50);
 
 delay(200);
 toggle = !toggle;
}
Re: why my code doesn't stay in loop?
Reply #2 - May 31st, 2009, 12:53am
 
The reason is that what you see on screen is the result of the whole draw() function: so you will see the total result of all drawing actions after a 400ms delay...
sw01 gave the right way to do this.
Re: why my code doesn't stay in loop?
Reply #3 - May 31st, 2009, 5:15pm
 
Thank you very much sw01 and PhiLho. Your answers helped a lot. The problem is solved!!


Thanks again!
Page Index Toggle Pages: 1