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

好的,我有一个跳跃按钮和一个运行时监听器函数用于当按下跳跃按钮时。 所以每当按下跳跃按钮时,我会对游戏英雄应用线性冲量。

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

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

谢谢

点赞
用户3197530
用户3197530

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

if(buttonpressed.id=="jump" and hero.y>display.contentHeight/2) then
    local vx, vy = hero:getLinearVelocity()
    if vy < velocity_threshold then -- 你需要定义这个值
        hero:applyLinearImpulse(0,-10,hero.x,hero.y)
    end
end

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

2016-08-10 05:49:46
用户4554496
用户4554496

添加一个跳跃计数器,当玩家和地面碰撞时重置,这样你以后就可以允许双重跳跃等。

2016-08-10 12:55:31
用户4244734
用户4244734

你也可以创建一个布尔值。 当跳跃开始时,布尔值为真。 当玩家与地面碰撞时为假。 当布尔值为真时,你不能再跳跃。

2016-08-26 11:08:45