performwithdelay not executing after specified interval

Hi, its my first post here, and i found the corona a really interesting framework to work with.

Scenario: I have an object which has touch event registered. i want the touch event to perform two different behaviours based on the the touch e.g short touch(like a tap) and long touch. if a user just tap it i want it to call one function other wise call another function. here is the code i have written to tackle this problem. However it is executing the climbDown only one time and not after the intervals specified. i know even if a long tap is presed it still execute climbUp().

Question: How to i use the same object to execute climbUp method when he just tap the object and to execute another method when he tap for long.

CODE:

local function countTaps(event)

     if event.phase == “began” then

       climbUp()

       timeClimb = timer.performWithDelay( 150, climbDown )

       return true

    elseif event.phase == “ended” then

      timer.cancel(timeClimb)

end

A tap is probably less than 150 ms in which case you’re potentially cancelling the timer before it has time to fire.

Rob

You could try something like this.  (I used something this in a  small game I was working on a while back)

this sample code should do what I think you are trying to do:

local beginTime = 0 local function climbUp() print("CLIMB UP") end local function climbDown() print("CLIMB DOWN") end local function onTouch(e) if e.phase == "began" then beginTime = system.getTimer() elseif e.phase == "ended" then diff = system.getTimer() - beginTime if diff \> 200 then climbDown() else climbUp() end end end local rect = display.newRect(100,100, 50, 50) rect:addEventListener("touch", onTouch)

system.getTimer() returns milliseconds

Good luck.

Bob

You will need to track the time Delta between began and ended and then perform whatever code depending on whether the delta is < 200 ms or not. This gives you the difference between a short touch and a long touch.

ok thanks for all the answers and your time, however i solved it using the existing code. which is

local function countTaps(event)

–timeClimb = timer.performWithDelay( 150, climbDown )

     if event.phase == “began” then

              --climbUp()

    timeClimb = timer.performWithDelay( 100, climbDown, 0)

       return true

    elseif event.phase == “ended” then

      timer.cancel(timeClimb)

end

i forgot to add 0 to the end of the timer.performWithDelay which lets you run the function infinite time unless cancelled. earlier it was only executing it once.

A tap is probably less than 150 ms in which case you’re potentially cancelling the timer before it has time to fire.

Rob

You could try something like this.  (I used something this in a  small game I was working on a while back)

this sample code should do what I think you are trying to do:

local beginTime = 0 local function climbUp() print("CLIMB UP") end local function climbDown() print("CLIMB DOWN") end local function onTouch(e) if e.phase == "began" then beginTime = system.getTimer() elseif e.phase == "ended" then diff = system.getTimer() - beginTime if diff \> 200 then climbDown() else climbUp() end end end local rect = display.newRect(100,100, 50, 50) rect:addEventListener("touch", onTouch)

system.getTimer() returns milliseconds

Good luck.

Bob

You will need to track the time Delta between began and ended and then perform whatever code depending on whether the delta is < 200 ms or not. This gives you the difference between a short touch and a long touch.

ok thanks for all the answers and your time, however i solved it using the existing code. which is

local function countTaps(event)

–timeClimb = timer.performWithDelay( 150, climbDown )

     if event.phase == “began” then

              --climbUp()

    timeClimb = timer.performWithDelay( 100, climbDown, 0)

       return true

    elseif event.phase == “ended” then

      timer.cancel(timeClimb)

end

i forgot to add 0 to the end of the timer.performWithDelay which lets you run the function infinite time unless cancelled. earlier it was only executing it once.