Page 1 of 1
How to detect a mouseUp during a loop
Posted: Mon Apr 22, 2013 11:01 pm
by rumplestiltskin
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
Re: How to detect a mouseUp during a loop
Posted: Mon Apr 22, 2013 11:36 pm
by Simon
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
Re: How to detect a mouseUp during a loop
Posted: Mon Apr 22, 2013 11:47 pm
by jmburnod
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
Re: How to detect a mouseUp during a loop
Posted: Tue Apr 23, 2013 12:04 am
by rumplestiltskin
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
Re: How to detect a mouseUp during a loop
Posted: Tue Apr 23, 2013 12:16 am
by Simon
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
Re: How to detect a mouseUp during a loop
Posted: Tue Apr 23, 2013 1:08 am
by rumplestiltskin
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
Re: How to detect a mouseUp during a loop
Posted: Tue Apr 23, 2013 3:54 am
by Simon
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
Re: How to detect a mouseUp during a loop
Posted: Tue Apr 23, 2013 4:13 am
by rumplestiltskin
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
Re: How to detect a mouseUp during a loop
Posted: Tue Apr 23, 2013 4:11 pm
by jacque
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