开发者

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 dra开发者_开发技巧gs 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜