Any precedence of built-in functions? (text(), delay()...etc...)

edited October 2017 in Questions about Code

Hi friends,

I am just wondering below code, a simple one:

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

void draw() {
  fill(255);
  text("hi",80,80);
  delay(1000);
  text("hi2",80,80);
  background(0);
  text("hi3",80,80);
}

My thought is that the window will show "hi", then wait for 1 second, show "hi2", clean up, then "hi3". However, what i see is: keep showing "hi3". What's more strange is that, when i go into debugger mode step by step, I find "hi" is not showing up at all! So i get confused if, within the draw() loop, there is precedence of these regular functions? Thanks to all!

Answers

  • The thing with draw() is that you never see the result until the end of draw(). If you use delay(), you are temporarily stopping draw() so, and when the time is up, draw() goes on as if nothing ever happened.

    So what is happening here? Draw displays "hi", then "hi2", then clears the screen, then displays "hi3" and then, and only then, do draw show you the result. Which is "hi3"

    It's like a painter who do a lot of paintstrokes, but you only get to see the result when the painter let you. So if you tell the painter to paint a cow, and then paint over it with black paint, yoy will never get to see the cow. Because the painter first paints the cow, then covers it up and then shows you the result: a black painting.

  • Hi Eeyorelife,

    I get your point. Thanks. But do you have any code suggestion if I just wanna do what my intended? (say, display an image and hold up for 1 second and clear screen)

  • You can make a timer. If you use millis(), you get the number of milliseconds since the sketch startet.

    void draw() {
      fill(255);
      text("hi", width/2, height/2);
      if (millis() >1000) {
        text("hi2", width/2, height/2);
      }
      if millis()>2000) {
        background(0);
        text("hi3", width/2, height/2);
      }
    } 
    
  • Thank you all. I integrated information by both of you, seems tackle my issue, and clear some looping concept in Processing (draw()).

  • Hi Chrisir, really thanks! Exact problem and well solved!

Sign In or Register to comment.