如何防止游戏无限跳跃? Corona LUA

时间:2016-08-09 08:36:01

标签: lua corona

好的,所以我有一个跳转按钮和一个运行时监听器功能,用于按下跳转按钮。 因此,当按下跳跃按钮时,我将线性冲动应用于游戏英雄。

local function controls(event)

 if(event.phase=="began") then

    if (buttonpressed.id=="jump") then

                hero:applyLinearImpulse(0,-10,hero.x,hero.y)
            end

 elseif(event.phase=="ended") then

 end

 end

现在的问题是,如果用户继续点击跳转按钮,那么英雄继续上升。我无法想到要解决这个问题。我能做的一件事就是将上面的代码更改为:

 local function controls(event)

 if(event.phase=="began") then

    if (buttonpressed.id=="jump" and hero.y>display.contentHeight/2) then

                hero:applyLinearImpulse(0,-10,hero.x,hero.y)
            end

 elseif(event.phase=="ended") then

 end

 end

但这仍然允许跳跃按钮工作,直到达到屏幕的一半。 请帮助我。

由于

3 个答案:

答案 0 :(得分:4)

添加一个跳跃计数器,当玩家与地板发生碰撞时重置,这样你可以在以后允许双跳等等。

答案 1 :(得分:0)

你可以检查英雄的速度,只有当速度低于某个阈值时才允许跳跃:

if (buttonpressed.id=="jump" and hero.y>display.contentHeight/2) then
    local vx, vy = hero:getLinearVelocity()
    if vy < velocity_threshold then -- you have to define this value
        hero:applyLinearImpulse(0,-10,hero.x,hero.y)
    end
end

这应该有效。使用linearDamping的另一种方法 (我不太了解日冕)。这导致线性运动的阻尼,这听起来像你可能需要的东西。

答案 2 :(得分:0)

您还可以创建布尔值。 当跳转开始时,布尔值为true。 当玩家与地板发生碰撞时是假的。 布尔值为true时,你不能再跳。

相关问题