如何在Corona SDK中检测到TouchOut事件? [英] How do I detect a TouchOut event in Corona SDK?
问题描述
在我的电晕游戏中,我在屏幕的角落放置了一个操纵杆图形.
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.
操纵杆图像通过control:addEventListener( "touch", onTouch )
订阅触摸"侦听器.
The joystick image subscribes to the "touch" listener with control:addEventListener( "touch", onTouch )
.
下面的操纵杆代码:
-- 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
有什么想法吗?谢谢!
推荐答案
添加:
if event.phase == "began" then
display.getCurrentStage():setFocus( control, event.id )
end
onTouch
函数的正文,以订阅触摸结束"事件,即使用户的手指不在操纵杆上也是如此.
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.
这篇关于如何在Corona SDK中检测到TouchOut事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!