Interrupting a loop

Scott Rossi scott at tactilemedia.com
Tue Feb 17 19:59:10 EST 2004


> Folks, maybe this is obvious and I'm too tired to see it, but what would be
> a good approach to the following problem?
> 
> In the app I'm developing have a kind of animated simulation which consists
> of 'repeat forever' loop that causes a series of actions (animations,
> sounds etc) to take place on the screen. The details don't matter, but the
> loop calls a lot of handlers in succession - some of which are quite
> complex - until it detects a particular condition and then it stops. A
> 'run' like this can go on for several minutes.

Actually, the details may matter because these could affect how you
interrupt the animation.


> What I want is to provide the user with a 'pause' button, which stops the
> looping action at the end of the last handler to be run (so the handlers
> called from within the repeat loop are kind of atomic in this context), and
> also provide a 'resume' button that gets things started where they left
> off. During the pause, the user is allowed to click various things on the
> screen etc and cause other handlers to run - so it's not just a case of
> freezing everything.
> 
> I can't immediately see an obvious way to do this, apart from getting the
> mouseUp event in the 'pause' button to set a flag and then test the flag at
> each step of the repeat loop (i.e. between each handler call). Given that
> the RR engine provides a very sophisticated message handling structure,
> this seems very primitive; and even if I do have this approach, I'm not
> sure how to handle the temporary exit from the repeat loop so as to be able
> to come back to it when 'resume' is triggered.


Test a global variable state (ie gPaused) or user property to establish the
pause within either of the following setups:

1) If you're using the built-in move command, make sure you use "without
waiting" so that other scripts/mouse events can be processed simultaneously.
To interrupt this type of animation, you need to use the movingControls
function to determine which objects to stop.

2) If you're using your own move construct, such as repeatedly setting the
location of an object during a repeat loop, consider adding a "wait XX with
messages" at the end of the loop (whatever time delay is appropriate) to
allow for other scripts/mouse events to be processed.

Note that unless you're moving a huge image (640x480, 24 bits), neither of
the above scripts should require multiple variable tests throughout the
script.  The Rev engine is pretty fast here.

Regards,

Scott Rossi
Creative Director
Tactile Media, Multimedia & Design
-----
E: scott at tactilemedia.com
W: http://www.tactilemedia.com



More information about the use-livecode mailing list