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 the code behavior
Page Index Toggle Pages: 1
Problem with the code behavior (Read 289 times)
Problem with the code behavior
Jan 14th, 2009, 9:46am
 
Hello, i m a beginner. I m trying to make a simple program to increase the area of a rectanlge continuously upto a limit as decided by me. I am just altering one side of the rect keeping other unchanged. While this code works fine;


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

int i=1;
void draw()
{  
  if (i<255)
  {
  rect(100,100,i,50);
  delay(10);
  }
i++;
}


The following code doesnt work as expected;

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

int i=2;
void draw()
{
for(int i=2; i<100; i++)
  {
  rect(100,100,i,50);
  delay(100);
  }
}  

It will be really great for a beginner like me if somebody could please check and explain whats wrong.
Re: Problem with the code behavior
Reply #1 - Jan 14th, 2009, 10:53am
 
I didn't test it but the first code increments once each draw, so u should see the change over time, in the second u try to do everything in one cycle..
So the first sketch is the way to go, the second only if you want to see immediate results, in which can the delay is also useless.

Re: Problem with the code behavior
Reply #2 - Jan 14th, 2009, 1:06pm
 
Thanks Kiza, but from what i read up, i can understand that to remove flicker, processing updates screen only when a new iteration of draw function begins. So the behavior that i see now makes sense.

But is there any way we can do it using for loop? This technique of updating screen which is followed by Processing in order to remove flicker is called as 'double buffer'. Anybody having hint how to implement it ourselves?

Shadab
Re: Problem with the code behavior
Reply #3 - Jan 14th, 2009, 1:29pm
 
Quote:
void setup()
{
  size(400,400);
  // Clear the screen
  background(0);
  
}

int i=2;
void draw()
{  
  // You don't clear the screen ever frame, assuming that's intentional? If not move the background(0); above to here.
  // Don't place for loops in draw() if you want each loop iteration to draw something, draw() only draws once per call.
  rect(100,100,i,50);
  delay(100);
  
  // Increment i without for loop
  i++;
  // If i has reached the limit (this was in the for loop)
  if(i == 100)
  {
    // reset the loop to the start (I'm assuming you want it to repeat?
    i = 2;
    // Better clear the screen or it will draw over itself
    background(0);
  }
}  


Re: Problem with the code behavior
Reply #4 - Jan 14th, 2009, 2:56pm
 
Hey thanks a lot stephen....works for me!
Page Index Toggle Pages: 1