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 does draw automatically loop in example
Page Index Toggle Pages: 1
Why does draw automatically loop in example (Read 676 times)
Why does draw automatically loop in example
Nov 1st, 2009, 7:26am
 
There are two very similar programs that are almost identical on processing 1.0.7 in the included examples section called Linear and SetupDraw, which show a horizontal line crawl up the window. This is Linear Code:
/**
* Linear Motion.
*
* Changing a variable to create a moving line.  
* When the line moves off the edge of the window,
* the variable is set to 0, which places the line
* back at the bottom of the screen.
*/

float a = 100;

void setup()
{
 size(640, 200);
 stroke(255);
}

void draw()
{
 background(51);
 a = a - 0.5;
 if (a < 0) {
   a = height;
 }
 line(0, a, width, a);  
}

I don't understand why the line will keep getting redrawn continuously to create the basic motion as there is no loop called, just an if conditional test. Unless it is because of the word void in that it won't return a variable value and changes the variable value inside so will keep looping, is that right? That is a bit subtle I reckon and should be made more explicit in the code, code comments and even the language reference section Any thoughts?
Re: Why does draw automatically loop in example
Reply #1 - Nov 1st, 2009, 7:55am
 
When you add a draw() method the Processing will repeatably call the draw() method without the user have to explicitly do so.

This enables simple animation as shown in this example.

Re: Why does draw automatically loop in example
Reply #2 - Nov 1st, 2009, 8:16am
 
When in doubt check the reference Wink
Re: Why does draw automatically loop in example
Reply #3 - Nov 1st, 2009, 8:24am
 
See also noLoop() Smiley
Re: Why does draw automatically loop in example
Reply #4 - Nov 1st, 2009, 8:25am
 
Oh my mistake I overlooked that point.
Page Index Toggle Pages: 1