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 & HelpPrograms › Problem with a simple If condition
Page Index Toggle Pages: 1
Problem with a simple If condition (Read 1215 times)
Problem with a simple If condition
Apr 14th, 2009, 1:24pm
 
Hi , i got this simple code , but the i have a problem with the if statement.


I want that when i press the mouse the strokeWeight gets bigger by one pixel everyframe so i did this :

 
  if ( mousePressed = true ){
   strokeWeight ++;    
 }

But the compiler says it canot find anything named strokeWeight ??? a built in function in Processing.

here's the code
Code:

void setup(){

size ( 300,300);
background (#FF0F4C);

}

void draw (){

smooth();
frameRate(12);
int step = height/10;
fill();
rect(0,0,width,height);




for ( int i=step ; i< height ; i+=step){
strokeWeight ( i*.1);
stroke (255);
line (20,i,width-20,i);

// this is the problematic part

if ( mousePressed = true ){
strokeWeight ++;
}
}
}


I also would like to know how to make the rectangle that i added to refresh the backgrounf not to show the borders , but if i add a noStroke to the code then the lines wont appear , what should i do ?

Thanx !
Re: Problem with a simple If condition
Reply #1 - Apr 14th, 2009, 1:51pm
 
There's 2 problems with your code.. one you need to use == not = and strokeWeight is a function, not a variable.

You want something like:

Code:
int weight=0;

//....

 for ( int i=step ; i< height ; i+=step){
   strokeWeight (weight+ (i*.1));
   stroke (255);
   line (20,i,width-20,i);
   
   // this is the problematic part
   
  if ( mousePressed == true ){
   weight++;    
 }
Re: Problem with a simple If condition
Reply #2 - Apr 15th, 2009, 12:20pm
 
Thanx !

But it still nt do what i want.

I want that when i press the mouse button, it makes the strokeWeight bigger.

Also i would like to delete the stroke of the rectangle of the background wich updates the background.

This is the code :

Code:
void setup(){

size ( 300,300);
background (#FF0F4C);

}

void draw (){

smooth();
frameRate(12);

//declare variables
int step = height/10;
int weight=0;

//update background color
fill(#FF0F4C);
rect(0,0,width,height);

// Algor
for ( int i=step ; i< height ; i+=step){
strokeWeight ( i*.1);
stroke (255);
line (20,i,width-20,i);

// this is the problematic part
if ( mousePressed == true ){
weight++;
}
}
}
Re: Problem with a simple If condition
Reply #3 - Apr 15th, 2009, 1:08pm
 
You can put smooth() and frameRate() calls in setup(), since these are global to the sketch. Not useful to call them on each frame.
Also you can call background() to clear the sketch's surface with a given color (but you can also use noStroke() if you want to keep using rect()).
Last point, weight variable is local to draw(), so all changes to its value are lost at the end of draw(). Declare it at the top of the sketch (before setup()).
Re: Problem with a simple If condition
Reply #4 - Apr 15th, 2009, 2:24pm
 
Thanx Philho,

But it  still dont increases the strokeWeight when i press the mouse , this is my code now with your tips.

Code:
void setup(){

 size ( 300,300);
 background (#FF0F4C);
smooth();
 frameRate(12);
}

void draw (){
 
 
 
 //declare variables    
int step = height/10;  
int weight=0;

//update background color  
background (#FF0F4C);
 
 // Algor
 for ( int i=step ; i< height ; i+=step){
   strokeWeight ( i*.1);
   stroke (255);
   line (20,i,width-20,i);
   
   // this is the problematic part  
if ( mousePressed == true ){
   weight++;    
 }
}
}
Re: Problem with a simple If condition
Reply #5 - Apr 15th, 2009, 3:23pm
 
I think you missed my last sentence... Smiley
Re: Problem with a simple If condition
Reply #6 - Apr 17th, 2009, 12:47pm
 
well actually i didnt, is just that i tried and it didnt work , look this is the code with the int weight variable is at the very top.


It still dont make the strokeWeight bigger, incrementally every frame, asi i want.


any other ideas ?

Here's the code with the weight variable is at the very top.

Code:
int weight=0;

void setup(){

size ( 300,300);
background (#FF0F4C);
smooth();
frameRate(12);
}

void draw (){



// Declare variables
int step = height/10;

// update background color
background (#FF0F4C);

// Algorithm
for ( int i=step ; i< height ; i+=step){
strokeWeight ( i*.1);
stroke (255);
line (20,i,width-20,i);

// this is the problematic part
if ( mousePressed == true ){
weight++;
}
}
}
Re: Problem with a simple If condition
Reply #7 - Apr 17th, 2009, 1:28pm
 
To increment it each frame, bring weight++; outside of the for loop.
Also, you are using the i variable as the base of your strokeWeight, not the weight variable.
Re: Problem with a simple If condition
Reply #8 - Apr 18th, 2009, 4:21pm
 
for ( int i=step ; i< height ; i+=step){
   strokeWeight ( i*.1);
   stroke (255);
   line (20,i,width-20,i);
   
      if ( mousePressed == true ){
   weight++;    
 }
}

at this part you should substitute the strokeWeight(i*.1) with strokeWeight(weight) to start applying the variable weight as the value of strokeWeight() function.

besides you should try placing your if statement outside the for loop for it to read at each draw cycle and not each for iteration.

so your code should probably look like this for it to work the way you want:

for ( int i=step ; i< height ; i+=step){
   strokeWeight (weight); //substituting (i*.1) with (weight)
   stroke (255);
   line (20,i,width-20,i);
}
// placing if outside for loop
if ( mousePressed == true ){
   weight++;    
}
Re: Problem with a simple If condition
Reply #9 - Apr 19th, 2009, 8:57am
 
THANX !

I have the new code like you wrote , but now it just shows very thin lines and it still dont increases when i press the mouse .  Embarrassed

any ideas ?

Code:
void setup(){

size ( 300,300);
background (#FF0F4C);
smooth();
frameRate(12);
}

void draw (){



// Declare variables
int step = height/10;
int weight=0;

// update background color
background (#FF0F4C);

// Algorithm
for ( int i=step ; i< height ; i+=step){
strokeWeight (weight); //substituting (i*.1) with (weight)
stroke (255);
line (20,i,width-20,i);
}
// placing if outside for loop
if ( mousePressed == true ){
weight++;
}
}
Re: Problem with a simple If condition
Reply #10 - Apr 19th, 2009, 9:34am
 
You need to declare your weight variable outside the draw.  You keep resetting your weight to 0 every time it draws, so it doesn't matter that you're pressing the mouse.  Place it before your setup.

Code:
 
int weight=0;

void setup(){

 size ( 300,300);
 background (#FF0F4C);
smooth();
 frameRate(12);
}

void draw (){

//   update background color  
background (#FF0F4C);

 //  Declare variables    
int step = height/10;

 //    Algorithm    
 for ( int i=step ; i< height ; i+=step){
  strokeWeight (weight); //substituting (i*.1) with (weight)
  stroke (255);
  line (20,i,width-20,i);
}
// placing if outside for loop
if ( mousePressed == true ){
  weight++;    
}
}



Page Index Toggle Pages: 1