Corona SDK:触摸事件

发布于 2025-01-02 00:52:34 字数 47 浏览 0 评论 0原文

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

How can I detect if screen is being touched? Seems to me, touch events are not generated while screen is touched and the finger is not moving.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

往日情怀 2025-01-09 00:52:34

是的,仅记录手指运动的变化。放下手指、抬起手指并拖动会触发事件。

Yes, only changes to the finger movement is recorded. Put down finger, lift up finger and dragging around triggers event.

好倦 2025-01-09 00:52:34

但是,您可以

 e.phase == "began"

在事件函数中执行此操作。当用户将手指放在屏幕上时就会触发该操作。

However, you could do

 e.phase == "began"

in your event function. That would be triggered when the user puts his finger on the screen.

赤濁 2025-01-09 00:52:34

触摸事件是分阶段处理的。因此,触摸生成的事件有“开始”、“移动”、“结束”和“取消”阶段。因此,您可以通过执行以下操作来使用检测:

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 行上的“self”更改为任何您希望监听触摸事件的对象

Touch events are handled in phases. So the event being generated by the touch has "began", "moved", "ended" and "cancelled" phases. You could use detection, therefore, by doing this:

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)

When you then need to check if the screen is being touched, simply do:

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

EDIT: Obviously you can change "self" on the addEventListener line to be any object you wish to listen for touch events on

假扮的天使 2025-01-09 00:52:34
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 )
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 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文