stopping a repeated script with a button

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Brittgriscom
Posts: 95
Joined: Wed Mar 30, 2011 10:15 am

Re: stopping a repeated script with a button

Post by Brittgriscom » Fri Apr 01, 2011 3:20 am

jmburnod wrote:Hi Magice
Is there an easy way to kill the loop before allowing the stack to close?
Yes. on CloseStack (or when you want)

Code: Select all

on closestack
   repeat for each line aLine in the pendingMessages
      if aLine contains "startTimer"  then 
         cancel item 1 of aLine
      end if
   end repeat
end closestack
Best

Jean-Marc
I made a pulsating diamond animation using a send in time loop, and I tried to make it stop with this code on a mouseUp handler, but the loop just wouldn't die! Is there any other way to make it stop? At the very least can I stop it in the message box somehow?

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: stopping a repeated script with a button

Post by bn » Fri Apr 01, 2011 8:41 am

Hi Britt,

could you post the code of your pulsating diamond that is called with send in time?

Jean-Marc's script should stop it.

Kind regards

Bernd

Brittgriscom
Posts: 95
Joined: Wed Mar 30, 2011 10:15 am

Re: stopping a repeated script with a button

Post by Brittgriscom » Mon Apr 04, 2011 4:02 am

In a player's callbacks:

12000,show graphic "Diamond"
12000,diamondAnimation


On Card:

on diamondAnimation
repeat with i = 1 to 20
set the blendLevel of graphic "Diamond" to i
-- I commented out the wait commands to make sure they weren't the problem
-- wait 1 with messages
end repeat
repeat with i= 20 down to 1
set the blendLevel of graphic "Diamond" to i
-- wait 1 with messages
end repeat
-- wait 1 with messages
send "diamondAnimation" to me in 300
end diamondAnimation



On Card:

on mouseUp
   repeat for each line aLine in the pendingMessages
      if aLine contains "diamondAnimation"  then --Do I need this? I don't have any other pending messages.
         cancel item 1 of aLine
      end if
   end repeat
end mouseUp

This doesn't cancel the messages.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: stopping a repeated script with a button

Post by bn » Mon Apr 04, 2011 11:52 am

Hi Britt,

I tried your script and changed it a bit. The way you posted it it was to fast to notice the blendlevel change.

Code: Select all

on diamondAnimation
   repeat with i = 1 to 50 step 2
      set the blendLevel of graphic "Diamond" to i
      wait 30 milliseconds
   end repeat
   repeat with i= 50 down to 1 step - 1
      set the blendLevel of graphic "Diamond" to i
      wait 30 milliseconds
   end repeat
   if "diamondAnimation" is not in the pendingMessages then
      send "diamondAnimation" to me in 2000 milliseconds
   end if
end diamondAnimation
Your cancle script works.

Code: Select all

on mouseUp
      repeat for each line aLine in the pendingMessages
            if aLine contains "diamondAnimation"  then --Do I need this? I don't have any other pending messages.
                  cancel item 1 of aLine
            end if
      end repeat
end mouseUp
--Do I need this? I don't have any other pending messages.
yes you should only cancel your own messages since Livecode also sends messages for housekeeping that would be canceled if you just delete all messages.

Notice that I added a conditional to the sending of the message: only if the message is not in the pendingMessages the new message is sent. Otherwise there could be a pile up of your message if something happens, e.g. the script is called twice or some Livecode/system slowdown happens. Just to make shure.

In your original script you issued a wait with messages statement. That is often a good idea. In your case it opens the execution up for new messages.

Suppose your script runs and there is no pendingMessage. With a wait with messages livecode would process the button script to cancel your pendingMessages. BUT the script then goes on and issues a new message "diamondAnimation". That would be a way for the button script to fail because of above. Now I took away the wait with messages and the button could only execute the mouseUP handler if the diamondAnimation handler is done, since during the execution of the diamondAnimation handler livecode is blocked. This is not a good idea since you animate the diamond for quite a while for the effect to become noticeable.

I propose a little variation of the theme:

Code: Select all

local sBlendLevelValue = 1 -- initial value of the blendlevel
local sGoUp = true -- initial Value of direction

on alternateDiamondAnimation
   if sGoUp then
      add 5 to sBlendLevelValue
      if sBlendLevelValue > 50 then
         put false into sGoUp
      end if
   else 
      subtract 5 from sBlendLevelValue
      if sBlendLevelValue <=0 then
         put 1 into sBlendLevelValue
         put true into sGoUp
      end if
   end if
   set the blendLevel of graphic "Diamond" to  sBlendLevelValue
   if "alternateDiamondAnimation" is not in the pendingMessages then
      if sBlendLevelValue = 1 then
         send "alternateDiamondAnimation" to me in 1000 milliseconds -- adjust
      else
         send "alternateDiamondAnimation" to me in 40 milliseconds -- adjust
      end if
   end if
end alternateDiamondAnimation
Notice the two script local variables "sBlendLevelValue" and "sGoUp" above the handler alternateDiamondAnimation. These store values between runs.

Now the handler finishes as fast as it can. The intervall of calling is controlled by the state of the blendlevel. If it is 1 then 1000 milliseconds else in 40 milliseconds. (by the way I prefer milliseconds because if you don't specify a unit when waiting it is ticks and a tick is 16 milliseconds. Confuses me.)

Now the alternateDiamondAnimation handler is not blocking at all and livecode stays responsive, since just setting the blendlevel is fast.

To stop the alternateDiamondAnimation handler just change your button script to reflect the new message name.

If you have questions regarding this please feel free to ask.

Kind regards

Bernd

If you want to post code there is the very convenient tab at the top of the forum text editor with "code" in it. It avoids possible problems with linebreaks and is easy to copy.

Brittgriscom
Posts: 95
Joined: Wed Mar 30, 2011 10:15 am

Re: stopping a repeated script with a button

Post by Brittgriscom » Mon Apr 04, 2011 9:14 pm

Works great. Thanks.

Post Reply