如何检测 Corona SDK 中的 TouchOut 事件?

发布于 2024-12-01 20:38:04 字数 896 浏览 6 评论 0原文

我在 Corona 游戏中的屏幕一角放置了一个操纵杆图形。

当用户触摸操纵杆并将其左右拖动时,角色就会移动。但是,如果用户从操纵杆的中间一直拖动到一侧,然后移开手指,角色就会继续移动。我希望角色在修饰时停止,即使修饰不再出现在操纵杆图形上。

操纵杆图像通过 control:addEventListener( "touch", onTouch ) 订阅“touch”监听器。

下面的操纵杆代码:

-- Constants
local playerSpeed = 300
local playerDamping = 15

-- Player controls
local onTouch = function( event )

    -- Player rotation
    local deltaX = event.x - control.x
    local deltaY = event.y - control.y
    local magnitude = math.sqrt( deltaX * deltaX + deltaY * deltaY )

    player.rotation = math.deg( math.atan2 ( deltaY, deltaX ) )

    -- Player speed
    if event.phase == "ended" then
        player.linearDamping = playerDamping
    else
        player.linearDamping = 0
        player:setLinearVelocity( deltaX / magnitude * playerSpeed, deltaY / magnitude * playerSpeed )
    end
end

有什么想法吗?谢谢!

I have a joystick graphic placed in the corner of the screen in my Corona game.

When the user touches the joystick and drags it from side-to-side, it moves the character. However, if the user drags from the middle of the joystick all the way off to the side, then removes his/her finger, the character keeps on moving. I'd like the character to stop on touch-up, even if the touch up is no longer on the joystick graphic.

The joystick image subscribes to the "touch" listener with control:addEventListener( "touch", onTouch ).

Joystick code below:

-- Constants
local playerSpeed = 300
local playerDamping = 15

-- Player controls
local onTouch = function( event )

    -- Player rotation
    local deltaX = event.x - control.x
    local deltaY = event.y - control.y
    local magnitude = math.sqrt( deltaX * deltaX + deltaY * deltaY )

    player.rotation = math.deg( math.atan2 ( deltaY, deltaX ) )

    -- Player speed
    if event.phase == "ended" then
        player.linearDamping = playerDamping
    else
        player.linearDamping = 0
        player:setLinearVelocity( deltaX / magnitude * playerSpeed, deltaY / magnitude * playerSpeed )
    end
end

Any ideas? Thanks!

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

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

发布评论

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

评论(1

梦里兽 2024-12-08 20:38:04

添加:

if event.phase == "began" then
    display.getCurrentStage():setFocus( control, event.id )
end

onTouch 函数的主体,以订阅“触摸结束”事件,即使用户的手指不在操纵杆上也是如此。

Add:

if event.phase == "began" then
    display.getCurrentStage():setFocus( control, event.id )
end

to the body of the onTouch function, to subscribe to the 'touch ended' event, even when the user's finger isn't on the joystick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文