How to detect a mouseUp during a loop

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
rumplestiltskin
Posts: 223
Joined: Wed Jun 21, 2006 7:33 pm
Contact:

How to detect a mouseUp during a loop

Post by rumplestiltskin » Mon Apr 22, 2013 11:01 pm

I've seen some topics discussing this but nothing that would resolve this problem.

Here's my loop:

Code: Select all

on mouseUp
   set the label of me to "Stop"
   repeat  until the mouseClick with messages
      move graphic "Oval" to the points of graphic "line" in .5 second
      play "click.aiff"
      wait .11 milliseconds with messages
   end repeat
   set the label of me to "Start"
end mouseUp
When I click the button, the label changes (as expected) and the movement and sound happens (as expected). But it seems that only clicking the mouse like crazy will, eventually, manage to get the handler to finish and exit. (The label of the button -does- change back.)

Suggestions will be gratefully accepted. Thanks,
Barry

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: How to detect a mouseUp during a loop

Post by Simon » Mon Apr 22, 2013 11:36 pm

Hi Barry,
That wait .11 mill is too fast. Screen refresh rate is at 60-70 kHz so 1 tick is about the fastest you could expect to see a change. 10 millisecs should be fine and give your mouseClick time to be seen (actually you could test this and push the millisecs up as high as you can).

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: How to detect a mouseUp during a loop

Post by jmburnod » Mon Apr 22, 2013 11:47 pm

Hi Barry,

I think you need something like that:

Code: Select all

on mouseUp 
   set the label of me to "Stop"
   put the points of graphic "line" into tPoints
   put the num of lines of tPoints into nbl
   put round(5000/nbl) into tDelay
   repeat  for each line tLoc in tPoints
      wait tDelay milliseconds with messages
      if the mouse is down then exit repeat
      --play "click.aiff"
      set the loc of grc "oval" to tloc
   end repeat 
   set the label of me to "Start"
end mouseUp
Best regards
Jean-Marc
https://alternatic.ch

rumplestiltskin
Posts: 223
Joined: Wed Jun 21, 2006 7:33 pm
Contact:

Re: How to detect a mouseUp during a loop

Post by rumplestiltskin » Tue Apr 23, 2013 12:04 am

Simon wrote:Hi Barry,
That wait .11 mill is too fast. Screen refresh rate is at 60-70 kHz so 1 tick is about the fastest you could expect to see a change. 10 millisecs should be fine and give your mouseClick time to be seen (actually you could test this and push the millisecs up as high as you can).

Simon
Whups! That was decimal I shouldn't have used. However, I tried the script with the "wait" as high as 1 second but it still doesn't work.

Also tried Jean-Marc's solution but it doesn't seem to do anything at all. No movement, nothing.

Maybe (probably) I'm not being clear enough.

My handler is -supposed- to change the label of the button from "Start" to "Stop" then start a loop moving a ball from one end of a straight line to the other (a total of two points, I guess). When the ball arrives at the end point, a click sound will play. Then the loop begins again. My problem is how to interrupt the loop when the user clicks the button again (which should cause an exit repeat and a change to the button label back to "Start".

Thanks

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: How to detect a mouseUp during a loop

Post by Simon » Tue Apr 23, 2013 12:16 am

Oh OK,
Try an if/then
If the label of me is "Stop" then
--do the stuff
else
set the label of me to "Start"
--do the stuuf
end if

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

rumplestiltskin
Posts: 223
Joined: Wed Jun 21, 2006 7:33 pm
Contact:

Re: How to detect a mouseUp during a loop

Post by rumplestiltskin » Tue Apr 23, 2013 1:08 am

Okay; I think I have it. Here's the script I'm using now:

Code: Select all

on mouseUp
   if the label of me is "Start" then
      set the label of me to "Stop"
      repeat until the label of me is "Start" with messages
         move graphic "ball" to the points of graphic "line" in .5 second
         play "click.aiff"
         wait 10 milliseconds
      end repeat
   end if
   set the label of me to "Start"
end mouseUp
It appears trying to trap for mouseClick just isn't working. But the if/then checking for the label of the button works fine.

Thanks for the suggestions. It all helped.

Barry

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: How to detect a mouseUp during a loop

Post by Simon » Tue Apr 23, 2013 3:54 am

Hi Barry,
I know you have the code working now but you might consider this method:

Code: Select all

local tMove
on mouseUp
   if the label of me is "Start" then
      set the label of me to "Stop"
      put true into tMove
      moveGraphic
   else
      put false into tMove
      set the label of me to "Start"
   end if
end mouseUp

on moveGraphic
   move graphic "ball" to the points of graphic "line" in .5 second
   play "click.aiff"
   if tMove = true then send "moveGraphic" to me in 10 milliseconds
end moveGraphic
There is a general idea not to use loops when a send-in-time will work. I'd really like to find a reference for that but I can't at the moment. Maybe someone will chime in.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

rumplestiltskin
Posts: 223
Joined: Wed Jun 21, 2006 7:33 pm
Contact:

Re: How to detect a mouseUp during a loop

Post by rumplestiltskin » Tue Apr 23, 2013 4:13 am

Simon,
Thanks for your assistance. Funny thing; I had written my script as two handlers - one of them using the "send" command almost exactly like yours and then determined I could do it all in one handler but, of course, I had to use the repeat loop. I'll change it back to what is essentially what you've suggested.

I remember I was doing some RunRev coding (way before LiveCode was named) when I needed to have about a dozen objects flying around the screen. I believe we used the send command to have the objects move around, bounce off the stack edges and a second handler responded to a mouseClick so we could interrupt the movement.

Until LiveCode puts in a "Do what I mean" button, I'll be posting questions. :)

Thanks again.
Barry

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: How to detect a mouseUp during a loop

Post by jacque » Tue Apr 23, 2013 4:11 pm

The "send" structure is the proper way to do this. LiveCode does not queue mouseclicks inside loops. The only way a mouseclick will trigger is if the user clicks at exactly the same moment as the engine is checking for it. Other clicks will go into the bit bucket.

Don't rely on "if the mouseclick" inside a loop. More general info here about polling the mouse:

http://www.hyperactivesw.com/polling.html
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply