William McCarty
William McCarty

Reputation: 839

Touch Hold Event In corona SDK

I was wondering how I would check if the user has touched the screen, but is holding their touch down and is not moving. Please help if you have anything I can go from. Ive been looking around and have yet to find anything to handle this.

Upvotes: 1

Views: 5317

Answers (2)

Doğancan Arabacı
Doğancan Arabacı

Reputation: 3982

You can use/modify this: (It's what Rob Miracle says)

local holding = false
local function enterFrameListener()
    if holding then
        -- Holding button
        -- Code here
        -- Code here
        -- Code here
    else
        -- Not holding
        -- Code here
        -- Code here
        -- Code here
    end
end

local function touchHandler( event )
    if event.phase == "began" then
        display.getCurrentStage():setFocus( event.target )
        event.target.isFocus = true
        Runtime:addEventListener( "enterFrame", enterFrameListener )
        holding = true
    elseif event.target.isFocus then
        if event.phase == "moved" then
        elseif event.phase == "ended" then
            holding = false
            Runtime:removeEventListener( "enterFrame", enterFrameListener )
            display.getCurrentStage():setFocus( nil )
            event.target.isFocus = false
        end
    end
    return true
end

I believe its obvious what touchHandler function is ^^

Upvotes: 5

Rob Miracle
Rob Miracle

Reputation: 3063

You will need a touch listener added to either an object covering your whole screen or you can add it to the system's RunTime.

See this guide: http://docs.coronalabs.com/guide/events/detectEvents/index.html#hit

Now, there are three "Phases" for these touch events. You get one when the press starts ("began"), one if the person moves their finger ("moved") and when they stop touching, there is an "ended" phase.

Depending on what you are trying to do, if you are say moving something while holding the button down, then you can set a flag like:

 if event.phase == "began" then
      pressed = true
 elseif event.phase == "ended" then
      pressed = false
 end

Then where ever you're moving you can check to see "if pressed then move".

Upvotes: 1

Related Questions