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 › Noob. Another question.
Page Index Toggle Pages: 1
Noob. Another question. (Read 813 times)
Noob. Another question.
Aug 17th, 2009, 11:59pm
 
I am trying to understand line by line how this code works before I move on.

Question 01. Within draw() it says x = 0;

Isn't this redundant? For at the top it says int x = 0;


Question 02. x = x + spacing;

Which spacing does spacing use? int spacing = 10; at the top or spacing = (mouseX / 2) + 1;

Thanks for advice.

int y = 80;        
int x = 0;        
int spacing = 10;  
int len = 20;      
int endLegs = 150;

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

void draw() {
 background(0);
 stroke(255);
 x = 0;

spacing = (mouseX / 2) + 1;
 while (x <= endLegs) {
   line(x,y,x,y + len);

   x = x + spacing;
 }
}
Re: Noob. Another question.
Reply #1 - Aug 18th, 2009, 12:34am
 
Quote:
Question 01. Within draw() it says x = 0;
Isn't this redundant? For at the top it says int x = 0;


The x declaration at the beginning of your code is there just to set it up once. Then x is set back to 0 everytime draw() restarts.

Quote:
Question 02. x = x + spacing;

Which spacing does spacing use? int spacing = 10; at the top or spacing = (mouseX / 2) + 1;


The spacing declaration at the beginning of your code is there just to set it up once. Then it will only use the spacing formula in draw().
Re: Noob. Another question.
Reply #2 - Aug 18th, 2009, 5:17am
 
Quote:
Question 01. Within draw() it says x = 0;
Isn't this redundant? For at the top it says int x = 0;


"int x = 0;" at the top of the sketch means : before the sketch windows appears, I tell you we're going to use a variable called x, which value is 0 for now. "int x;" would do the same job, since the initial value of an int variable is 0 by default.

"x = 0;" in the draw loop means : each time the window is redrawn, set x to 0. Since x changes later in the draw() method (x = x + ...), it is useful to set it back to 0.
Re: Noob. Another question.
Reply #3 - Aug 18th, 2009, 11:47am
 
I think I am understanding this better now. draw() draws at maybe 30 frames per second. The first sweep or frame of draw() draws all of the vertical lines spaced apart by 1 pixel. Only one pixel because my mouse has not yet moved over the window. I thought draw() drew just one line per frame. This is why it needs to reset x = 0 in draw().

int spacing = 10 at the top can be any number even 0. The numbers have no effect, but int spacing must be there.
Re: Noob. Another question.
Reply #4 - Aug 18th, 2009, 12:05pm
 
Yes. Be sure you understand what variable scope mean :
http://processing.org/learning/basics/variablescope.html
Re: Noob. Another question.
Reply #5 - Aug 18th, 2009, 4:04pm
 
I read that article. It seems as though all variables must be declared at the top irregardless of whether they are used as global variables.

In my case, I have two local variables. X and spacing. The use of these two over rides the global X and spacing variables at the top.

So the only globals being called upon are y, len and endLegs.
Re: Noob. Another question.
Reply #6 - Aug 19th, 2009, 6:46am
 
Lenticular wrote on Aug 18th, 2009, 4:04pm:
I read that article. It seems as though all variables must be declared at the top irregardless of whether they are used as global variables.


Not at all - you can declare a variable locally within a function, without declaring it at the top, meaning its value can then only be accessed from within the function.  e.g. if you moved "int x = 0" into setup the value of x would only be accessible from within setup and you would get an error.  By declaring it at the top you make it global and therefore accessible from within setup, draw etc...

Lenticular wrote on Aug 18th, 2009, 4:04pm:
In my case, I have two local variables. X and spacing. The use of these two over rides the global X and spacing variables at the top.

So the only globals being called upon are y, len and endLegs.


No - the x and spacing variables are global.  I'm not sure you've understood the distinction between:

 int x = 0;

and

 x=0;

The first (when you specify what type of variable it is) declares the variable - where you put this is important as it defines the scope of the variable.  The second simply assigns a value to the variable, can only be done within the scope of the variable and thus can't be done until the variable has been declared.  So when you use that in draw you're simply changing the value, not declaring the variable.

To make matters a little more confusing you can declare variables with the same name in different scopes, so the value returned will obviously depend on the point at which you access that variable name:

Code:
int x = 0;        

void setup() {
size(200,200);
int x = 13;
// prints the local variable x
println("(setup) x = " + x);
}

void draw() {
// prints the global variable x
 println("(draw) x = " + x);
 noLoop();
}


That might either make things clearer or confuse you more.  There are times when it's perfectly valid to do this, but you might want to avoid it until you've got the idea of scope clear in your head...
Re: Noob. Another question.
Reply #7 - Aug 19th, 2009, 2:38pm
 
Thanks for clearing that up. I think I understand now.
Page Index Toggle Pages: 1