使用 Corona SDK 实现多点触控的奔跑和跳跃

我觉得这应该很容易,但我找不到任何解决方法。通常只是使用 system.enable("multitouch") 然后使用 setFocus() 方法。 我有一个右箭头按钮响应触摸事件,按住按钮让玩家向右奔跑。然后我有一个跳跃按钮,响应点击事件,让玩家跳跃。 奔跑、跳跃都能正常运行,但当我奔跑时想跳跃时就没反应了。 下面是一些代码:

local speed = 3
local motionx = 0
-- 当右箭头被触摸时,向右移动角色
    function right:touch()
        if string.find(player.sequence,"jump") then
            player:setSequence("jumpRight")
            motionx = speed;
        elseif string.find(player.sequence,"duck") then
            player:setSequence("duckRight")
        else
            player:setSequence("goingRight")
            motionx = speed;
        end
        player:play()
    end
    right:addEventListener("touch",right)

    -- 当上箭头被点击时,跳跃角色
    function jump:tap()
        if not isJumping then
            if string.find(player.sequence, "goingRight") then
                isJumping = true
                player:setSequence("jumpRight")
                player:setLinearVelocity(0,-120)
            end
            if string.find(player.sequence, "goingLeft") then
                isJumping = true
                player:setSequence("jumpLeft")
                player:setLinearVelocity(0,-120)
            end
            if string.find(player.sequence, "duckLeft") then
                player:setSequence("goingLeft")
            end
            if string.find(player.sequence, "duckRight") then
                player:setSequence("goingRight")
            end
            player:play()
        end
    end
    jump:addEventListener("tap",jump)

    -- 移动角色
    local function movePlayer (event)
        player.x = player.x + motionx;
    end
    Runtime:addEventListener("enterFrame", movePlayer)

我不知道在哪里、如何使用 setFocus() 方法。但目前当奔跑时,我必须松开右按钮才能跳跃。

点赞
用户2951900
用户2951900

这是我的错误,我想使用 TAP 事件来实现跳跃。我猜因为它被称为 multiTOUCH,所以只能用多个 TOUCH 事件 :) 我将 TAP 事件改成了 TOUCH 事件,并添加了以下代码:

if event.phase == "began" then
        display.getCurrentStage():setFocus( event.target, event.id )
        --other code
end
if event.phase == "ended" or event.phase == "cancelled" then
        display.getCurrentStage():setFocus( event.target, nil )
end

现在它运行得很好。

2017-02-18 10:51:13