Repeat while mouseDown / touchStart?

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
RalphORama
Posts: 7
Joined: Wed Oct 22, 2014 5:19 pm

Repeat while mouseDown / touchStart?

Post by RalphORama » Thu Feb 26, 2015 5:56 am

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?

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

Re: Repeat while mouseDown / touchStart?

Post by dunbarx » Thu Feb 26, 2015 7:05 am

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

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

Re: Repeat while mouseDown / touchStart?

Post by jacque » Thu Feb 26, 2015 10:43 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: Repeat while mouseDown / touchStart?

Post by SparkOut » Thu Feb 26, 2015 11:00 pm

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.

RalphORama
Posts: 7
Joined: Wed Oct 22, 2014 5:19 pm

Re: Repeat while mouseDown / touchStart?

Post by RalphORama » Fri Feb 27, 2015 3:26 am

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!

RalphORama
Posts: 7
Joined: Wed Oct 22, 2014 5:19 pm

Re: Repeat while mouseDown / touchStart?

Post by RalphORama » Fri Feb 27, 2015 5:05 pm

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?

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: Repeat while mouseDown / touchStart?

Post by magice » Fri Feb 27, 2015 5:24 pm

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.
Last edited by magice on Fri Feb 27, 2015 5:44 pm, edited 2 times in total.

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

Re: Repeat while mouseDown / touchStart?

Post by dunbarx » Fri Feb 27, 2015 5:27 pm

Try "wait with messages"

Craig

Post Reply