Limit touch Event

Not sure how to approach this. I have a ball with a touch listener attached. You touch and hold the ball and move it around the screen until released. Is there a way to limit the amount of time the ball is held. So after the a period of time the touch is cancelled even if the user is still touching? 

Hope this makes sense?

Yup, it makes sense. It’s easily doable.

Ok. First of all you should have a touch listener, say, “touchListener”.

If it doesn’t already exist, add a "   if event.phase == ‘began’   " statement in the touch function. In this statement, just add : 

myTimer = timer.performWithDelay(10000, function() --whatever you want local event event.phase = "ended" myTouchFunction(event) myTimer = nil end,1)

Don’t forget to declare myTimer at the beginning of your code ( local myTimer )

Also, add this in your "   event.phase == ‘ended’   " block :

if myTimer ~= nil then timer.cancel(myTimer); myTimer = nil end --Code to release ball

Yup, it makes sense. It’s easily doable.

Ok. First of all you should have a touch listener, say, “touchListener”.

If it doesn’t already exist, add a "   if event.phase == ‘began’   " statement in the touch function. In this statement, just add : 

myTimer = timer.performWithDelay(10000, function() --whatever you want local event event.phase = "ended" myTouchFunction(event) myTimer = nil end,1)

Don’t forget to declare myTimer at the beginning of your code ( local myTimer )

Also, add this in your "   event.phase == ‘ended’   " block :

if myTimer ~= nil then timer.cancel(myTimer); myTimer = nil end --Code to release ball