Drawing a specific object less frequently than every frame

edited July 2016 in Questions about Code

This questions lies somewhere between a question about programming and a question about code. Let me know if I'm in the wrong place.

I'm trying to draw an object once every few seconds (or 300 frames) rather than every frame. The code below is basically what I've been using, the variables are declared before setup and the if statement is in draw.

int counter = 0;
int ix = 300;

    if (counter < ix) {
    counter = counter+1;
  } else if (counter > ix) {
    //draw the thing
  } else if (counter >= ix) {
    counter = counter - ix;
  }  

I've tried it a bunch of different ways and can't seem to get it. As soon as the counter reaches ix it doesn't reset. It stays at 300. Am I making to complicated? Is there an easier way to do this? Should I be using one variable instead of two?

I don't think I have a good handle on operators and how boolean works vs. (whatever the alternative to boolean is). I've tried using both int and float. What's the difference between counter+1 and counter++?

Any help is appreciated. Thanks.

Tagged:

Answers

  • Answer ✓

    When counter is 299 the first part of the condition is true so counter is now 300

    Next time through the first part is false, the second part is false (300 is not > 300) but the third part is true, so counter is reset to 0.

    The middle part is never true...

    Is there an easier way to do this?

    Yes.

    if ((framecount % 300) == 0) {
      // draw
    }
    

    Framecount auto increments every frame. The above will be true on frame 300, 600, 900 etc

    (But this probably isn't what you really want)

    What's the difference between counter+1 and counter++

    counter = counter + 1;
    counter += 1;
    counter++;
    

    all amount to the same thing

  • Thanks koogs, frameCount worked exactly like I wanted (and it's much simpler).

    I fixed my code as you suggested and it drew the object but still didn't set the counter back to zero. I tried this too:

    else if (counter >= ix) {
       counter = 0;
    

    rather than using

    counter = counter - ix 
    

    but no luck. I don't need it now, but do you have any idea why it's wrong still?

  • void setup(){
      size(200,200);
      fill(255);
    }
    
    void draw(){
      background(0);
      if(frameCount % 300 == 0){
        background(255,0,0);
      }
      text(frameCount, 20, 20);
    }
    
  • Answer ✓

    I really like the idea posted here before using the mod operator aka %. Related to your initial post, this is how I would modify your code to reset the counter:

       //////////NEXT not needed....
      ////   if (counter < ix) {
       ////    counter = counter+1;
      ////   } else 
    
     counter++;
     if (counter > ix) {
    
        //draw the thing        
        //Now reset the counter
        counter=0;
      } 
    
       //////////NEXT not needed....
      ////   else if (counter >= ix) {
      ////     counter = counter - ix;
      ////   } 
    

    There is another way which I just described recently in the following post: [https://forum.processing.org/two/discussion/17308/how-to-make-the-image-appear-for-a-few-moment#latest](Holding an image for few seconds)

    More resources which could apply to your application: https://forum.processing.org/two/search?Search=millis()

    I hope this helps,

    Kf

  • edited July 2016

    If you want an explicit counter to go from 0 to n (exclusive) repeatedly then just use

    counter = (counter + 1) % n;
    
Sign In or Register to comment.