Android Corona SDK多点触控跑步和跳跃
我认为这应该很容易做到,但我无法找到任何解决方案。始终只需启用system.enable(“multitouch”),然后使用setFocus()方法。 我有一个右键,它对触摸事件做出反应,并使玩家在被按住时能够正确地运行。然后我有一个跳转按钮,它对点击事件做出反应,使玩家跳转。 跑步有效,跳跃有效,但当我跑步并尝试跳跃时,什么也没发生。 下面是一些代码:Android Corona SDK多点触控跑步和跳跃,android,mobile,lua,coronasdk,Android,Mobile,Lua,Coronasdk,我认为这应该很容易做到,但我无法找到任何解决方案。始终只需启用system.enable(“multitouch”),然后使用setFocus()方法。 我有一个右键,它对触摸事件做出反应,并使玩家在被按住时能够正确地运行。然后我有一个跳转按钮,它对点击事件做出反应,使玩家跳转。 跑步有效,跳跃有效,但当我跑步并尝试跳跃时,什么也没发生。 下面是一些代码: local speed = 3 local motionx = 0 -- When right arrow is touched,
local speed = 3
local motionx = 0
-- When right arrow is touched, move character right
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)
-- When up arrow is tapped, jump character
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)
-- Move character
local function movePlayer (event)
player.x = player.x + motionx;
end
Runtime:addEventListener("enterFrame", movePlayer)
我不知道应该在哪里、如何或为什么使用setFocus()方法。但到目前为止,在跑步时,我无法跳转,直到我松开右键。这是我的错误,我想使用轻触事件进行跳转。我猜,由于它被称为多点触摸,它只适用于多个触摸事件:)我将点击事件更改为触摸事件,并添加:
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
现在它工作得很好