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 › bug when using delay.
Page Index Toggle Pages: 1
bug when using delay. (Read 2822 times)
bug when using delay.
Feb 8th, 2010, 6:10pm
 
hi everyone,

I got the strangest error and I'm pretty sure there is no problem with my coding, this looks like a bug inside processing.

here is my code :
void setup()
{
  size(800,600);
}

void draw()
{
 fill(255,0,0);
 rect(10,10,100,100);
 println("red");
 delay(1000);
 fill(0,255,0);
 rect(10,10,100,100);
 println("green");
 delay(1000);
}

This is the simplest code ever, there is supposed to be a square that flashes between red and green while printing "red" or "green".  But the thing is the square flashes only one time while keeping on printing "red" and "green".  My guess is that processing doesn't like delays (it works well with mousePressed and other stuff).

here, we are all mystified with this problem.

thanks for helping,

nico.
Re: bug when using delay.
Reply #1 - Feb 8th, 2010, 7:16pm
 
The screen isn't updated until the end of the draw() method.

What you are doing is drawing one thing, pausing, and then drawing another thing - which all appears as a single "frame" in the display that just happened to take ages to draw.

What you really want to do is draw different things in different frames, so the draw() method needs to make a decision about what to draw, not draw both things.

How your method makes the decision is up to you - maybe based on time passing (millis()), maybe based on the number of frames (frameCount) since the start, maybe based on a user keypress, mouse movement, and so on.

-spxl
Re: bug when using delay.
Reply #2 - Feb 9th, 2010, 1:53am
 
This Topic was moved here from Software Bugs by PhiLho.

1) Please, read the section descriptions, particularly in this section: "This board is an archive, post to the bugs database."
2) Please, read the stickies on each section:
- All new posts here will be deleted
- No really! Use the bugs DB instead
- PLEASE USE THE BUGS DB INSTEAD OF POSTING HERE
Looks explicit enough, no?
3) Please, don't claim there is a bug in Processing before checking with other. Most of the time, as it is the case here, the bug is in the code, in the usage of Processing.
And this problem is actually a FAQ.

Thanks.
Re: bug when using delay.
Reply #3 - Feb 9th, 2010, 3:21am
 
SubPixel is right
Quote:
What you are doing is drawing one thing, pausing, and then drawing another thing - which all appears as a single "frame" in the display that just happened to take ages to draw.


Insted of using delay you can use a timer.

create a class that will continuously check the current run time (millis()) versus a saved entry point.

This example was one I used from Daniel Shiffman's book "Learning Processing"

Code:

class Timer  {
 int savedTime;  //  When Timer started
 int totalTime;    //  How long Timer should last
 
 Timer (int tempTotalTime)  {
   totalTime = tempTotalTime;
 }
 
 //  Starting the timer
 void start ()  {
   savedTime = millis();  //  When the Timer starts it stores the current time in milliseconds
 }
 
 boolean isFinished ()  {
   //  Check how much time has passed
   int passedTime = millis() - savedTime;
   if (passedTime > totalTime)  {
     return true;
   }  else  {
     return false;
   }
 }
 
}


When you have established this class you can create an instance of the class Timer:

       Timer timer;

Initialize it in Setup or where ever you desire:

       timer = new Timer(5000);
       timer.start();

And use an if statement in draw that checks if the timer has finished:

       if (timer.isFinished())  {
         background(random(255));
         timer.start();
       }

You can also change the method in which the timer keeps track of time

Quote:
How your method makes the decision is up to you - maybe based on time passing (millis()), maybe based on the number of frames (frameCount) since the start, maybe based on a user keypress, mouse movement, and so on.
Re: bug when using delay.
Reply #4 - May 13th, 2010, 10:30pm
 
bbq9000 wrote on Feb 9th, 2010, 3:21am:
SubPixel is right
Quote:
What you are doing is drawing one thing, pausing, and then drawing another thing - which all appears as a single "frame" in the display that just happened to take ages to draw.


Insted of using delay you can use a timer.

create a class that will continuously check the current run time (millis()) versus a saved entry point.

This example was one I used from Daniel Shiffman's book "Learning Processing"

Code:

class Timer  {
 int savedTime;  //  When Timer started
 int totalTime;    //  How long Timer should last
 
 Timer (int tempTotalTime)  {
   totalTime = tempTotalTime;
 }
 
 //  Starting the timer
 void start ()  {
   savedTime = millis();  //  When the Timer starts it stores the current time in milliseconds
 }
 
 boolean isFinished ()  {
   //  Check how much time has passed
   int passedTime = millis() - savedTime;
   if (passedTime > totalTime)  {
     return true;
   }  else  {
     return false;
   }
 }
 
}


When you have established this class you can create an instance of the class Timer:

       Timer timer;

Initialize it in Setup or where ever you desire:

       timer = new Timer(5000);
       timer.start();

And use an if statement in draw that checks if the timer has finished:

       if (timer.isFinished())  {
         background(random(255));
         timer.start();
       }

You can also change the method in which the timer keeps track of time

Quote:
How your method makes the decision is up to you - maybe based on time passing (millis()), maybe based on the number of frames (frameCount) since the start, maybe based on a user keypress, mouse movement, and so on.













I dont understand this:
When you have established this class you can create an instance of the class Timer:

      Timer timer;

can you give me some example code?
Thanks
Re: bug when using delay.
Reply #5 - May 13th, 2010, 11:16pm
 
The example code is in the response you quote...
Technically,
Timer timer;
doesn't create an instance of the class, it declares a variable of that type.
In setup(), you create the instance, ie. you make a new object of that type (there can be several):
timer = new Timer(5000); // Timer for 5s
then you start it:
timer.start();
And in draw(), you use it to check if time is reached:
if (timer.isFinished())  {
 // Do something expected at this time
 // ...
 // Reset the timer for a new delay
 timer.start();
}


I wanted to show how to use that with your code above, using states, as I often write.
Then it evolved a bit, showing a less trivial example, so I can point here to illustrate how to use these states.. Smiley
Quote:
int state;
Timer timer;

void setup()
{
  size(500, 500);
  smooth();
  ellipseMode(CORNERS);
  noStroke();
  PFont font = createFont("Arial", 24);
  textFont(font);
  
  state = 0;
  timer = new Timer(2); // Wait 2 seconds before starting
  timer.start();
  ShowState();
}

void draw()
{
  if (timer.isFinished())
  {
    // Time to change state
    switch (state)
    {
      case 0: // Initial state
        state = 1;
        timer.start(1);
        break;
      case 1: // Middle state
        state = 2;
        timer.start(0.5);
        break;
      case 2: // Last state
        state = 0;
        timer.start(2); // Back to 2s
        break;
    }
    // For state, I could have done state = ++state % 3;
    // but complexer behavior (depending on user input for example)
    // is possible

    ShowState();    
  }
  // Handle drawing, depending on state
  switch (state)
  {
    case 0:
      fill(0, 0, 255);
      ellipse(10, 10, 110, 110);
      //println("blue");
      break;
    case 1: //
      fill(255, 0, 0);
      rect(10, 10, 100, 100);
      //println("red");
      break;
    case 2: //
      fill(0, 255, 0);
      rect(20, 20, 80, 80);
      //println("green");
      break;
  }
}

class Timer {
  float savedTime;  // When Timer started
  float totalTime;  // How long Timer should last

  Timer(float pTotalTime)  {
    totalTime = pTotalTime;
  }

  //  Starting the timer
  void start() {
    savedTime = millis() / 1000.0; // When the Timer starts it stores the current time in seconds
  }
  
  // Starting the timer by changing its total time
  void start(float pTotalTime)  {
    totalTime = pTotalTime;
    start();
  }
  
  boolean isFinished() {
    //  Check how much time has passed
    float passedTime = millis() / 1000.0 - savedTime;
    return passedTime > totalTime;
  }
  
  float getDelay() {
     return totalTime;
  }
}

// Show current state
void ShowState()
{
  int posX = 180, posY = 60;
  fill(255);
  rect(posX, posY, 50, 50);
  fill(0);
  textSize(24);
  text("" + state, posX + 20, posY + 25);
  textSize(12);
  text("" + timer.getDelay(), posX + 20, posY + 45);
}

I changed a bit the Timer class to make it more flexible (restart with a different time) and more intuitive (give times in seconds).
Page Index Toggle Pages: 1