Draw looping status -- best way of checking?

What is the best way of checking on the current Processing loop()/noLoop() status?

I have encountered situations in which I want to toggle looping rather than set it -- a simple example is: press space to pause/un-pause. I couldn't find anything in the language reference on looping status -- e.g. nothing referenced from loop() or noLoop().

  • isLooping(): in the Processing javadocs I found the PApplet.isLooping() method -- however, I'm not sure if this is undocumented / do-not-use.
  • if(looping): in a bug report I found mention of a "looping" flag. Checking it works -- I can even set it directly, and it appears to have the same effect as loop() / noLoop() -- but I couldn't locate it in the github source, and it seems very undocumented...?

I went ahead and created a sketch that demonstrates four methods of toggling the draw loop by pressing 1 / 2 / 3 / 4. All four methods work.

boolean myLooping = true;

int bgBlue = 0;
void draw(){
    background( 0, 0, bgBlue );
    bgBlue = (bgBlue+3)%255;
}

void keyPressed(){
    switch(key){
        case '1':
            if(myLooping){ noLoop(); }
            else { loop(); }
            myLooping = !myLooping;
            break;
        //// check the `isLooping()` method
        case '2':
            if(isLooping()){ noLoop(); }
            else { loop(); }
            break;
        //// check `looping` and set with loop()/noLoop()
        case '3':
            if(looping){ noLoop(); }
            else { loop(); }      
            break;
        //// flip `looping` directly
        case '4':
            looping = !looping;
            break;
    }
}

Just writing looping = !looping; is the most concise, but it is also the most undocumented. Given how complex the looping model is in Processing, any advice on advantages / drawbacks to using any of these methods (or others not mentioned here) to detect the looping status or to toggle it?

Answers

  • Answer ✓

    Honestly, I would not rely on any internal function/value to track this. I would keep track of it myself. So just do what you did in case 1.

  • edited September 2016 Answer ✓

    Variable looping always existed. So it's pretty safe to use it. Unless they decide to private it later!

    But be warned that if you decide to deploy your sketch to the web via Pjs, those undocumented stuff won't work! :-@

Sign In or Register to comment.