How to freeze a draw loop?

I have a draw loop that makes a new graphic each time. I want to be able to pause the loop by clicking the mouse when I see a graphic that I like. But everything I’ve tried fails to stop on the current frame. It’s always the next frame, or a later one, no matter how slow the frame rate. For example,

if (mousePressed == true) { delay(20000); }

pauses things for 20 seconds, but it seems to take a frame or two until things stop.

I haven’t found any info that addresses this sort of thing…I’m doing something wrong. Suggestions?

// Discourse.Processing.org/t/how-to-freeze-a-draw-loop/2741/2
// GoToLoop (2018-Aug-18)

static final int DELAY = 5 * 1000; // 5 seconds
static final float FPS = .5; // 2 seconds

void setup() {
  size(300, 150);
  frameRate(FPS);
}

void draw() {
  getSurface().setTitle("Frame: " + frameCount);
  background((color) random(#000000));
}

void mousePressed() {
  pauseFrame();
}

void pauseFrame() {
  delay(DELAY);
}

You can use loop() and noLoop(). You can find hem in the reference. Another way, which is not documented in the reference, is to do this:

void mousePressed(){
   looping =  !looping;  //EDIT******
}

This toggles between loop and noLoop, exactly as I suggested in the first line. however, this way is more straight forward. Not sure if it will stop right at the frame that you want. Just give it a try and see if it works for you.

Kf

1 Like

This works! Thank you!

I get an error: Syntax error on token"!=", invalid AssignmentOperator

But the other suggestion works. Thanks!

1 Like

Should be instead: looping = !looping; or looping ^= true; :nerd_face:

This seems to stop it on the next frame, not the current one. :face_with_raised_eyebrow:

My mistake… I will correct my post.

We will need to see some code to see if it is possible to store your current state, or use another approach different to looping.

Kf

void mousePressed(){looping=!looping;}