Page 1 of 1

Repeat while mouseDown / touchStart?

Posted: Thu Feb 26, 2015 5:56 am
by RalphORama
I've run into an issue when working on a personal project. I'd like to repeat a loop while the mouse is down on a button, and terminate it when the mouseUp message is passed. I can't figure out how.

My code:

Code: Select all

on mouseDown
   touchStart
end mouseDown
on mouseUp
   touchEnd
end mouseUp

on touchStart
   repeat until touchEnd -- Problem area
      wait 100 milliseconds
      incrementScore
   end repeat
end touchStart

on incrementScore
   add 1 to scoreProgress -- add 1 to the score
   set the thumbPosition of scrollbar "progress" to scoreProgress
   put scoreProgress & "!" into fld "timerLbl" of grp "center"
end incrementScore
This kills the program. The loop doesn't terminate once the mouse goes up or the touch event ends. How can I fix this?

Re: Repeat while mouseDown / touchStart?

Posted: Thu Feb 26, 2015 7:05 am
by dunbarx
Hi.

It looks like you have been watching and listening, since it seems to me like you are trying to use fairly advanced techniques. This is a good thing, and I bet your project will need such things soon.

But in the meantime, make one button and one field on a card. In the button script:

Code: Select all

on mouseDown
   repeat until the mouse is up
      add 1 to fld 1
      wait 5
   end repeat
     answer "Your are DONE"
end mouseDown
Simpler than you thought?

It is hard to think simply at first. Your thinking is sound, but you have syntactical errors which are in fact "not experienced with LiveCode" errors. For example, the line:
repeat until touchend
sounds logical, but LC evaluates that as:
repeat until touchend is "true"
and not as
"repeat until the "touchEnd" message is sent
But do write back when you need another boost.This all falls into place if you keep at it.

Craig Newman

Re: Repeat while mouseDown / touchStart?

Posted: Thu Feb 26, 2015 10:43 pm
by jacque
Besides what Craig said, don't do this:

Code: Select all

on mouseDown
   touchStart
end mouseDown
on mouseUp
   touchEnd
end mouseUp
MouseUp and touchEnd are the same thing, as are mouseDown and touchStart. Both versions of each event are sent on mobile, so when you do the above you are creating two mouseDowns in succession, and two mouseUps in succession. You will get two calls to each handler.

It usually isn't necessary to use these touch events. Just use mouseDown and mouseUp, which work everywhere. The touch events are useful if you are scripting multi-touch behaviors like pinch/zoom. In that case you do need to use touch because it provides an ID for each finger tap. But for a single-tap button it isn't needed.

Re: Repeat while mouseDown / touchStart?

Posted: Thu Feb 26, 2015 11:00 pm
by SparkOut
For a built-in option for the handling of a mouse button being held down over an object, see also the "mouseStillDown" message in the Dictionary. You would need to adjust the score increment according to the idleRate and idleTicks, so - other options are easier (as indicated above). Still, it's what I imagine you were imagining when you tried to interpret built in messages in your trial handlers.

Re: Repeat while mouseDown / touchStart?

Posted: Fri Feb 27, 2015 3:26 am
by RalphORama
dunbarx wrote:

Code: Select all

on mouseDown
   repeat until the mouse is up
      add 1 to fld 1
      wait 5
   end repeat
     answer "Your are DONE"
end mouseDown
This worked like a charm! Thank you very much for your help and other advice!

Re: Repeat while mouseDown / touchStart?

Posted: Fri Feb 27, 2015 5:05 pm
by RalphORama
Sorry for the double post, I've run into another issue with the same handler. I used the "until mouse is up" trick above, and added a wait to it (nothing else needs to be happening while this is going on, so it was the easiest way to achieve what I wanted). However, I've run into a strange issue.

Code: Select all

on mouseDown
   put 0 into scoreProgress
   
   repeat until the mouse is up 
      send incrementScore to me in barSpeed milliseconds
      -- wait, then add to the score
   end repeat
   
   mouseUp
end mouseDown

on incrementScore
   add 1 to scoreProgress -- add 1 to the score
   set the thumbPosition of scrollbar "progress" to scoreProgress
   put scoreProgress & "!" into fld "timerLbl" of grp "center"
end incrementScore
What I'm trying to achieve is the following:
I have a loading ("scroll") bar. When the player holds down the button, the thumbPosition is incremented every 100 milliseconds (fills up slowly). However, with my current code, everything freezes at 1. When I let go, the bar jumps up to a larger number, depending on how long I held down the click. If I take out the 'wait' command, it fills up in real time, really fast. How can I fix this?

Re: Repeat while mouseDown / touchStart?

Posted: Fri Feb 27, 2015 5:24 pm
by magice
It is doing exactly what it is being told to do. It is creating a message to be sent every "barSpeed" milliseconds. However because the repeat loop is executing, those messages can''t. So when the repeat loop finishes they all get executed at once. Actually the best way to do this, is to eliminate the repeat if you are going to use send in time. Instead add to your handler:

Code: Select all

if the mouse is down then send incrementScore to me in 100 milliseconds. 
That way it calls itself if the mouse is still down.

Re: Repeat while mouseDown / touchStart?

Posted: Fri Feb 27, 2015 5:27 pm
by dunbarx
Try "wait with messages"

Craig