Touch Hold Event In corona SDK

橙三吉。 提交于 2020-01-15 15:56:28

问题


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.


回答1:


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 ^^




回答2:


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".



来源:https://stackoverflow.com/questions/16473220/touch-hold-event-in-corona-sdk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!