2016-07-07 67 views
0

我研究過如何在slick2d中實現基礎重力系統。這裏是我有的代碼(這是在更新功能):在Slick2D中跳躍難度

if (input.isKeyDown(Input.KEY_UP)) { 
     spressed = true; //Has the UP key been pressed? 
    } 
    if (spressed) { 
     if (!sjumping) {//if so, are we already in the air? 
      Sub_vertical_speed = -1.0f * delta;//negative value indicates an upward movement 
      sjumping = true;//yes, we are in the air 
     } 
     if (sjumping) { //if we're in the air, make gravity happen 
      Sub_vertical_speed += 0.04f * delta;//change this value to alter gravity strength 
     } 
     Sub.y += Sub_vertical_speed; 
    } 
    if (Sub.y == Sub.bottom){//Sub.bottom is the floor of the game 
     sjumping = false;//we're not jumping anymore 
     spressed = false;//up key reset 
    } 

這裏是問題出現的地方。當我按下向上鍵時,精靈跳躍並正常下降,但再次按下向上鍵不起作用。我原本以爲這是因爲我沒有重新設置,所以我添加了行來將它設置爲false,但是你仍然只能跳一次。 :/

回答

1

它看起來像你的Sub.y需要被固定到你的Sub.bottom,所以它不會超出它。嘗試:

if(Sub.y >= Sub.bottom) { 
    Sub.y = Sub.bottom; 
    sjumping = false; 
    spressed = false; 
} 
+0

謝謝!但對於閱讀此內容的人來說,它需要> =,而不是<= – Steampunkery

+0

是的,沒有意識到您的最低價值更大。我做了適當的編輯。也請標記爲答案:) – Steve

0

我已經做了類似的事情之前,這裏我的假設是,Sub.y總是不等於Sub.bottom。根據y位置和垂直速度,對象的y位置將永遠不會是Sub.bottom的值。下面的代碼將測試這個:

if (Sub_vertical_speed + Sub.y > Sub.bottom){ //new position would be past the bottom 
    Sub.y = Sub.bottom; 
    sjumping = false; //we're not jumping anymore 
    spressed = false; //up key reset 
}