Page 1 of 1

Stop Button Help

Posted: Mon Nov 18, 2013 5:48 pm
by calmrr3
Hello,

I have a start button which executes the following script:

Code: Select all

global tNumber
on mouseUp
   startTiming
   add 1 to tNumber
end mouseUp

   on startTiming
   
   lock screen for visual effect
   
   repeat with z = 1 to 27
   
   repeat with y = 1 to 27
      put y into line y of temp
   end repeat
   sort temp by random(10000) 
   
   repeat with y = 1 to 27
   set the points of graphic ("Line "  & z) to the loc of graphic ("tCircle" & z) & return & the loc of graphic ("tCircle" &  line y of temp)
       end repeat
     
   set the visible of graphic ("Line "  & z)  to not the visible of graphic ("Line "  & z)

   If the optionKey is down then
      show graphic ("Line " & z)
      exit to top
   end if
   send "startTiming" to me in random(90) + 30 ticks
 end repeat
unlock screen with visual effect dissolve very fast
end startTiming
Beside the Start Button I would like a button that stops it (Currently holding the optionKey does this)

Re: Stop Button Help

Posted: Mon Nov 18, 2013 6:02 pm
by dunbarx
Well, you have a recursive handler running, and if you see that the line that checks for the state of the optionKey determines whether to continue or not, can you think of a way to implement that with a button press on the card?

Look up the "is within" operator, and consider that in conjunction with the "mouseLoc" function and the "rect" of an object. Oh, and the "mouse" function. You can do this.

Craig

Re: Stop Button Help

Posted: Mon Nov 18, 2013 6:41 pm
by Klaus
Hi calmrr3,

looks like you want to CANCEL the pending message named "startTiming" :D
Do this: Add global variable that will hold that message ID:

Code: Select all

global tStartTimingMessageID

 on startTiming   
  lock screen for visual effect
  repeat with z = 1 to 27
    repeat with y = 1 to 27
      put y into line y of temp
    end repeat
    sort temp by random(10000)
    repeat with y = 1 to 27
         set the points of graphic ("Line "  & z) to the loc of graphic ("tCircle" & z) & return & the loc of graphic ("tCircle" &  line y of temp)
           end repeat
    set the visible of graphic ("Line "  & z)  to not the visible of graphic ("Line "  & z)
    # If the optionKey is down then
    # show graphic ("Line " & z)
    # exit to top
    # end if
    send "startTiming" to me in random(90) + 30 ticks
    ## Now store the MESSAGE ID of the "send..." operation:
    put the result into tStartTimingMessageID
  end repeat
  unlock screen with visual effect dissolve very fast
end startTiming
Then you can later do this in your STOP button:

Code: Select all

# global tStartTimingMessageID
on mouseup
  cancel tStartTimingMessageID
end mouseup
Hope I understood your problem right 8)


Best

Klaus

Re: Stop Button Help

Posted: Mon Nov 18, 2013 7:19 pm
by dunbarx
What Klaus said. He is introducing a fabulous new way to stop your recursive handler. The "send in time" command sets up a queue of "pending' messages, and these can be accessed and cancelled. This is actually a cleaner and more direct way to do what you need. The important thing lurking here is that since control is returned to the user while all this is running, you have access to that queue and can modify it.

But still do it the old fashioned way, as I hinted earlier, as it is a method more in the trenches, so to speak, albeit old-fashioned.

Craig

Re: Stop Button Help

Posted: Tue Nov 19, 2013 3:48 pm
by calmrr3
I think I may be doing something wrong, The animation starts fine but I have to force quit LiveCode to get it to stop. Maybe i've applied the script in the wrong place:

Stack Script:

Code: Select all

global tStartTimingMessageID

 on startTiming   
  lock screen for visual effect
  repeat with z = 1 to 27
    repeat with y = 1 to 27
      put y into line y of temp
    end repeat
    sort temp by random(10000)
    repeat with y = 1 to 27
         set the points of graphic ("Line "  & z) to the loc of graphic ("tCircle" & z) & return & the loc of graphic ("tCircle" &  line y of temp)
           end repeat
    set the visible of graphic ("Line "  & z)  to not the visible of graphic ("Line "  & z)
    # If the optionKey is down then
    # show graphic ("Line " & z)
    # exit to top
    # end if
    send "startTiming" to me in random(90) + 30 ticks
    ## Now store the MESSAGE ID of the "send..." operation:
    put the result into tStartTimingMessageID
  end repeat
  unlock screen with visual effect dissolve very fast
end startTiming
Start button Script:

Code: Select all

on mouseUp
   startTiming
end mouseUp
Stop button Script

Code: Select all

global tStartTimingMessageID
on mouseup
  cancel tStartTimingMessageID
end mouseup

Re: Stop Button Help

Posted: Tue Nov 19, 2013 4:57 pm
by Klaus
Hmmm, just made a little test stack with tis simple stack script:

Code: Select all

global tStartTimingMessageID
on startTiming   
    add 1 to fld 1
    send "startTiming" to me in 1 secs
    ## Now store the MESSAGE ID of the "send..." operation:
    put the result into tStartTimingMessageID
end startTiming
And these two buttons "Start" and "Stop" with scripts as in your last example
and everything works as exspected.

No idea, sorry, anything else in your script(s)?

Re: Stop Button Help

Posted: Tue Nov 19, 2013 5:37 pm
by calmrr3
I've tried to attach the file,

I think it has something to do with a repeat loop.. but thats just a guess really!

Re: Stop Button Help

Posted: Tue Nov 19, 2013 5:51 pm
by Klaus
HI,

ouch, you generated 27 "startTiming" messages per cycle :D

Put it outside the repeat loop and that's it:

Code: Select all

global tStartTimingMessageID

on startTiming
   lock screen for visual effect
   repeat with z = 1 to 27
      repeat with y = 1 to 27
         put y into line y of temp
      end repeat
      sort temp by random(10000)
      repeat with y = 1 to 27
         set the points of graphic ("Line "  & z) to the loc of graphic ("tCircle" & z) & return & the loc of graphic ("tCircle" &  line y of temp)
      end repeat
          set the visible of graphic ("Line "  & z)  to not the visible of graphic ("Line "  & z)
   end repeat

   ## NOW :-)
   send "startTiming" to me in random(90) + 30 ticks
   put the result into tStartTimingMessageID
   unlock screen with visual effect dissolve very fast
end startTiming
Best

Klaus

Re: Stop Button Help

Posted: Tue Nov 19, 2013 5:54 pm
by calmrr3
Ahh I see,

Thanks once again for your help!