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 › While Loop Question
Page Index Toggle Pages: 1
While Loop Question (Read 705 times)
While Loop Question
May 2nd, 2010, 3:18pm
 
I am attempting Exercise 6-1 in 'Learning Processing'.

Could someone please explain why the line in red "float cDia = width*2;"wouldn't work on it's own.

The program worked fine after I added the blue line "cDia = width*2;" in setup.



float cDia = width*2;
float spacing = 20;
float colourChange = 30;
float cFill = 200;

void setup(){
cDia = width*2;
size(200,200);
background(255);
}

void draw(){

 ellipseMode(CENTER);

 while(cDia >= 0){
   stroke(0);
   fill(cFill);
   ellipse(width/2,height/2,cDia,cDia);
   cDia = cDia - spacing;
   cFill = cFill - colourChange;
   println("colour: " + cFill);
   println("Circle: " + cDia);
 }  
}
Re: While Loop Question
Reply #1 - May 2nd, 2010, 3:42pm
 
Simple - 'width' is defined when you call size() in setup, so you can't refer to it before setup()...  Technically the blue line shouldn't work either as it's also before size(), which is why size() should always be the first call in setup...
Re: While Loop Question
Reply #2 - May 2nd, 2010, 5:00pm
 
Thanks blindfish.
It seems so obvious now that you have explained it.
Page Index Toggle Pages: 1