Slick2D 中的跳跃难度

Jumping difficulty in 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
    }

问题就出在这里。当我按下向上键时,精灵正常跳跃和下降,但再次按下向上键没有任何反应。我本来以为是因为我没有reset spressed,所以加了一行设置为false,但是还是只能跳一次。 :/

看起来您的 Sub.y 需要固定在您的 Sub.bottom 上,这样它就不会超过它。尝试:

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

我以前做过类似的事情,我在这里的假设是 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
}