Interrupting a loop

Ken Ray kray at sonsothunder.com
Tue Feb 17 21:25:08 EST 2004


Graham,

Here's a way I've used in the past:

-- Script of btn "Start":
global gPaused,gExit
on mouseUp
  put false into gPaused
  put false into gExit
  repeat forever
    -- code that does what you want
    wait 0 secs with messages  -- allows processing of other scripts
    if gPaused then
      wait until gPaused is false with messages
    end if
    if gExit then exit repeat
  end repeat
  answer "Done!"
end mouseUp

-- Script of btn "Pause":
on mouseUp
  global gPaused
  put true into gPaused
end mouseUp	

-- Script of btn "Continue":
on mouseUp
  global gPaused
  put false into gPaused
end mouseUp	

-- Script of btn "Stop":
on mouseUp
  global gExit
  put true into gExit
end mouseUp	

HTH,

Ken Ray
Sons of Thunder Software
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/ 
> -----Original Message-----
> From: use-revolution-bounces at lists.runrev.com 
> [mailto:use-revolution-bounces at lists.runrev.com] On Behalf Of 
> Graham Samuel
> Sent: Tuesday, February 17, 2004 5:49 PM
> To: use-revolution-request-lists.runrev.com
> Subject: Interrupting a loop
> 
> 
> 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.
> 
> 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.
> 
> I'd be grateful for any ideas.
> 
> TIA
> 
> Graham
> 
> ---------------------------------------------------
> Graham Samuel / The Living Fossil Co. / UK & France  
> 
> 
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com 
> http://lists.runrev.com/mailman/listinfo/use-> revolution
> 




More information about the use-livecode mailing list