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 › Do I need to pass variable to the function
Page Index Toggle Pages: 1
Do I need to pass variable to the function? (Read 1775 times)
Do I need to pass variable to the function?
Jun 7th, 2010, 8:34pm
 
I create the 'umbrella' function inside the drawOneCircle function. However, it doesn't seem to work.

Code:

import controlP5.*;

ControlP5 controlP5;
boolean pause = false;
int backgroundColor = color(200);

void setup(){
smooth();
size(1600,800);
frameRate(10);

controlP5 = new ControlP5(this);
control();

reset();

}

void reset(){
background(backgroundColor);
}

void draw() {
if (pause == false){
drawOneCircle();
}
}

void drawOneCircle(){

int posx = int(random(0,width));
int posy = int(random(0,height));
float radius = random(10,50);

noStroke();
fill(0,10);
ellipse(posx,posy,radius*4,radius*4);
ellipse(posx,posy,radius*3,radius*3);
ellipse(posx,posy,radius*2.5,radius*2.5);

fill(255);
ellipse(posx,posy,radius*2,radius*2);

umbrella();

}

void umbrella(){

stroke(0);
for(float i = 0.0; i < 2.0*PI; i+=0.5){
line(posx,posy,posx+sin(i)*radius,posy+cos(i)*radius);
}

}








Re: Do I need to pass variable to the function?
Reply #1 - Jun 8th, 2010, 12:56am
 
Looks like a scope issue.  Once I comment out your 'control()' function in setup, which isn't included in your code, I get a "Cannot find anything named 'posx'" error.  'posx' is a variable defined within the drawOneCircle() function.  Just because you call umbrella() from within the function doesn't mean it has access to local variables.  You could simply add appropriate parameters to the umbrella function and pass these when you call it from drawOneCircle:

Code:
import controlP5.*;

ControlP5 controlP5;
boolean pause = false;
int backgroundColor = color(200);

void setup(){
 smooth();
 size(1600,800);
 frameRate(10);

 controlP5 = new ControlP5(this);
 //control();

 reset();

}

void reset(){
 background(backgroundColor);
}

void draw() {
 if (pause == false){
   drawOneCircle();
 }
}

void drawOneCircle(){

 int posx = int(random(0,width));
 int posy = int(random(0,height));
 float radius = random(10,50);

 noStroke();
 fill(0,10);
 ellipse(posx,posy,radius*4,radius*4);
 ellipse(posx,posy,radius*3,radius*3);
 ellipse(posx,posy,radius*2.5,radius*2.5);

 fill(255);
 ellipse(posx,posy,radius*2,radius*2);
 
 umbrella(posx, posy, radius);

}

void umbrella(int posx, int posy, float radius){

 stroke(0);
 for(float i = 0.0; i < 2.0*PI; i+=0.5){
   line(posx,posy,posx+sin(i)*radius,posy+cos(i)*radius);
 }
}


Then you also have the option of calling an umbrella without drawing a circle; which you must want to do if you're separating the code into a separate function...
Re: Do I need to pass variable to the function?
Reply #2 - Jun 8th, 2010, 9:08am
 
Thank you. It helps. I forgot to paste other tabs to the same code. Is there anyway that I could copy and paste in this forum if you have several tabs?

Here is the whole code:
Code:

import controlP5.*;

ControlP5 controlP5;
boolean pause = false;
int backgroundColor = color(200);

void setup(){
 smooth();
 size(1600,800);
 frameRate(10);

 controlP5 = new ControlP5(this);
 control();

 reset();

}

void reset(){
 background(backgroundColor);
}

void draw() {
 if (pause == false){
   drawOneCircle();
 }
}

void drawOneCircle(){

 int posx = int(random(0,width));
 int posy = int(random(0,height));
 float radius = random(10,50);

 noStroke();
 fill(0,10);
 ellipse(posx,posy,radius*4,radius*4);
 ellipse(posx,posy,radius*3,radius*3);
 ellipse(posx,posy,radius*2.5,radius*2.5);

 fill(255);
 ellipse(posx,posy,radius*2,radius*2);

 //umbrella(posx, posy, radius);
}

void umbrella(int posx, int posy, float radius){
 stroke(0);
 for(float i = 0.0; i < 2.0*PI; i+=0.5-radius/200){
   line(posx,posy,posx+sin(i)*radius,posy+cos(i)*radius);
 }
}


void keyPressed(){
 if(key == 'p'){

   pause = true;

 }
 if(key == 'r'){
   
  pause = false;
 
 }


}

void control(){  
 //background
 controlP5.addSlider("sliderA",0,255,backgroundColor,20,20,80,20).setId(1);
 controlP5.controller("sliderA").setLabel("changeBackground");
 controlP5.controller("sliderA").setMax(255);
 controlP5.controller("sliderA").setMin(0);
}

void sliderA(){
 int Color = (int)random(255);
 backgroundColor = color(Color);
 //println("### bang(). a bang event. setting background to "+ Color);
 controlP5.controller("sliderA").setValue(Color);
}


void controlEvent(ControlEvent theEvent) {
 switch(theEvent.controller().id()) {
   case(1):
   backgroundColor = (int)(theEvent.controller().value());
   reset();
   break;
 }

}




Re: Do I need to pass variable to the function?
Reply #3 - Jun 8th, 2010, 9:15am
 
I'm not aware of a method to copy paste all the code in one go.

Do you actually have a question regarding this latest code?
Page Index Toggle Pages: 1