2016-08-09 68 views
0

好吧,所以我有一個跳轉按鈕和一個運行時監聽器功能,當按下跳轉按鈕時。 所以當按下跳轉按鈕時,我會將線性衝動應用於遊戲英雄。如何防止遊戲中無限跳躍?電暈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 

但這將仍然允許跳躍按鈕的工作達到一半的屏幕,直到。 請幫助我。

感謝

回答

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。這導致線性運動的阻尼,這聽起來像您可能需要的東西。

4

添加一個跳躍計數器,當玩家與地板發生碰撞時重置,這樣,如果您想要,您可以稍後允許雙跳等。

0

您也可以創建一個布爾值。 跳轉開始時,布爾值爲true。 當玩家與地板碰撞時是假的。 雖然布爾值爲true,但不能再次跳轉。