Corona SDK:触摸事件

时间:2012-02-04 13:04:19

标签: sdk lua touch corona

如何检测屏幕是否被触摸?在我看来,触摸屏幕并且手指没有移动时不会生成触摸事件。

4 个答案:

答案 0 :(得分:0)

是的,仅记录手指移动的更改。放下手指,抬起手指并拖动触发事件。

答案 1 :(得分:0)

但是,你可以做到

 e.phase == "began"

在您的活动中。当用户将手指放在屏幕上时会触发。

答案 2 :(得分:0)

触摸事件分阶段处理。因此,触摸生成的事件已经“开始”,“移动”,“结束”和“取消”阶段。因此,您可以使用检测:

self.isTouched = false;

function defaultTouchHandler(e)
    if(e.phase == "began") then
        print("Tapped")
        self.isTouched = true;
        --User has touched the screen (not moving). Do "onMouseDown" things here
    elseif(e.phase == "moved") then
        print("Moved")
        --User is moving their finger wile touching. Do "onMouseMoved" things here
    elseif(e.phase == "cancelled" or e.phase == "ended") then
        print("End of touch")
        self.isTouched = false;
        --User lifted their finger, or an interrupt happened. Do "onMouseUp" things here
    end
end

self:addEventListener("touch", defaultTouchHandler)

当您需要检查是否触摸屏幕时,只需执行以下操作:

if(isTouched) then
    --Screen is being touched
else
    --Screen is not being touched
end

编辑:显然,您可以将addEventListener行上的“自我”更改为您希望收听触摸事件的任何对象

答案 3 :(得分:0)

local object = display.newImage( "ball.png" )
object.id = "ball object"

 local function onObjectTouch( event )
if ( event.phase == "began" ) then
    print( "Touch event began on: " .. event.target.id )
elseif ( event.phase == "ended" ) then
    print( "Touch event ended on: " .. event.target.id )
end
return true
end
object:addEventListener( "touch", onObjectTouch )