Stop Button Help

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

Post Reply
calmrr3
Posts: 100
Joined: Mon Oct 28, 2013 2:39 pm

Stop Button Help

Post by calmrr3 » Mon Nov 18, 2013 5:48 pm

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)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Stop Button Help

Post by dunbarx » Mon Nov 18, 2013 6:02 pm

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

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Stop Button Help

Post by Klaus » Mon Nov 18, 2013 6:41 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Stop Button Help

Post by dunbarx » Mon Nov 18, 2013 7:19 pm

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

calmrr3
Posts: 100
Joined: Mon Oct 28, 2013 2:39 pm

Re: Stop Button Help

Post by calmrr3 » Tue Nov 19, 2013 3:48 pm

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

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Stop Button Help

Post by Klaus » Tue Nov 19, 2013 4:57 pm

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)?

calmrr3
Posts: 100
Joined: Mon Oct 28, 2013 2:39 pm

Re: Stop Button Help

Post by calmrr3 » Tue Nov 19, 2013 5:37 pm

I've tried to attach the file,

I think it has something to do with a repeat loop.. but thats just a guess really!
Attachments
WithButtons.livecode.zip
(3 KiB) Downloaded 238 times

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Stop Button Help

Post by Klaus » Tue Nov 19, 2013 5:51 pm

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

calmrr3
Posts: 100
Joined: Mon Oct 28, 2013 2:39 pm

Re: Stop Button Help

Post by calmrr3 » Tue Nov 19, 2013 5:54 pm

Ahh I see,

Thanks once again for your help!

Post Reply