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?jmburnod wrote:Hi Magice
Yes. on CloseStack (or when you want)Is there an easy way to kill the loop before allowing the stack to close?BestCode: 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
Jean-Marc
stopping a repeated script with a button
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Posts: 95
- Joined: Wed Mar 30, 2011 10:15 am
Re: stopping a repeated script with a button
Re: stopping a repeated script with a button
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
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
-
- Posts: 95
- Joined: Wed Mar 30, 2011 10:15 am
Re: stopping a repeated script with a button
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.
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.
Re: stopping a repeated script with a button
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.
Your cancle script works.
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:
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.
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
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
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.--Do I need this? I don't have any other pending 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
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.
-
- Posts: 95
- Joined: Wed Mar 30, 2011 10:15 am
Re: stopping a repeated script with a button
Works great. Thanks.