如何检测 Corona SDK 中的 TouchOut 事件?
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加:
到
onTouch
函数的主体,以订阅“触摸结束”事件,即使用户的手指不在操纵杆上也是如此。Add:
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.