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 › forloop vs. ifloop ... different behavior
Page Index Toggle Pages: 1
forloop vs. ifloop ... different behavior (Read 599 times)
forloop vs. ifloop ... different behavior
Jun 10th, 2008, 10:49am
 
I had some hours of work last night figuring out why this loop doesnt work the way i wanted it. And solved the problem with an "if loop". But i still dont get where the difference is and why "if" works but "for" doesnt. Just for the understanding, does anybody knows why this is?

This is just an example loop but it clearly shows the problem.

The following one produces an endless number of screenshots with the last step of the loop.
Code:

void setup(){
size(300,300);
}
void draw() {
for(int i=1; i<=10; i++){
background(255);
fill(0);
ellipse(10*i,10*i,10,10);

saveFrame("screenshot-####.png");
}
}



The next one does what it should. It produces 10 shots and moves the ellipse between each shot and stops when its done

Code:

void setup(){
size(300,300);
}
int i=1;
void draw() {

if(i<=10){
background(255);
fill(0);
ellipse(10*i,10*i,10,10);

saveFrame("screenshot-####.png");
i=i+1;
}
}



Does anybody knows?
Re: forloop vs. ifloop ... different behavior
Reply #1 - Jun 10th, 2008, 11:20am
 
draw is called every frame, having a loop that draws multiple frames inside it doesn't make a lot of sense.

the second one works because you've made i global rather than local so it isn't being initialised to 1 everytime draw is called. (also, that second one isn't a loop)

void setup(){
size(300,300);
}

// draw one frame of 10 ellipses
void draw() {
background(255);
fill(0);    
for(int i=1; i<=10; i++){
  ellipse(10*i,10*i,10,10);
}
saveFrame("screenshot-####.png");
}
Re: forloop vs. ifloop ... different behavior
Reply #2 - Jun 10th, 2008, 11:44am
 
Thanks for your reply, but the code you just showed me doesnt do what i wanted to do. Also the first screenshot shows the 10 circles and it doesnt stop making shots after 10.

But still, if the way ive done it with the "ifloop" is not the right way, does anybody know how it should be done?
Re: forloop vs. ifloop ... different behavior
Reply #3 - Jun 10th, 2008, 12:26pm
 
you can do this :

Code:
void setup() {
size(300,300);
fill(0);
for(int i=1; i<=10; i++) {
background(255);
ellipse(10*i,10*i,10,10);
saveFrame("screenshot-####.png");
}
}


or as you did in the second sketch.

but using a for loop in the draw() method doesn't make sense, as koogs said. actually, speaking of an if loop doesn't make sense either : the draw() method loops, but the if statement is just a condition test (and by the way the second sketch never stops, the draw() method is still called 30 times/second, or so, even if nothing happens on the screen).
Re: forloop vs. ifloop ... different behavior
Reply #4 - Jun 10th, 2008, 12:41pm
 
Thanks, i guess i finally understand that putting a loop in the draw method doesnt make sense. But the code you showed me doesnt work either. It just make one shot of the last position. Actually it behaves the same like my forloop except it stops after the first shot, and i guess thats just because its in the setup and not in the draw method. But shouldnt it draw a circle make a shot, go back to the beginning of the loop, countup "i", draw the moved circle again, make another shot and so on? why doesnt it do it that way ? Maybe im just to blind to see it...
Re: forloop vs. ifloop ... different behavior
Reply #5 - Jun 10th, 2008, 12:46pm
 
Actually, your second code is the correct one: the loop is around the draw function, somehow.
It is just the natural way to do this in Processing.

And loops in draw make sense: to draw multiple objects! Wink
Re: forloop vs. ifloop ... different behavior
Reply #6 - Jun 10th, 2008, 12:50pm
 
oh ok, I got it : the problem you face comes from the saveFrame() method. the 10 images are produced, but have the same name, so the same file is overwritten 10 times and, as a result, you've got the last image as screenshot-0000.png. this is because the frame counter (that helps naming your image files) is increased at each draw() call only, not at each saveFrame() call.

try this instead :
Code:
save("screenshot-"+i+".png"); 

Re: forloop vs. ifloop ... different behavior
Reply #7 - Jun 10th, 2008, 12:51pm
 
Quote:
And loops in draw make sense: to draw multiple objects! ;)

Yes, sure. :-)

I should have said : in that case ! :-p
Re: forloop vs. ifloop ... different behavior
Reply #8 - Jun 10th, 2008, 12:56pm
 
Isnt that what i do? draw different objects ? Anyway, now i starts to make sense, i didnt know that it only counts up everytime draw is repeated, i thought everyshot gets another number but as i know it i can now tell why it doesnt work. Thanks to both of you!
Re: forloop vs. ifloop ... different behavior
Reply #9 - Jun 10th, 2008, 1:18pm
 
Quote:
Isnt that what i do? draw different objects ?


well, you're drawing an ellipse, then you are erasing the background and drawing an ellipse again. it's kinda like drawing the same object at different positions.

by drawing multiple objects using a for loop, I think we meant displaying many different objects contained in an array.
Page Index Toggle Pages: 1