Slick2D的跳跃难度

时间:2016-07-07 18:25:24

标签: java slick2d gravity

我研究过如何在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
    }

这是问题出现的地方。当我按下向上键时,精灵跳跃并正常下降,但再次按向上键不会做任何事情。我原本以为是因为我没有重置spressed,所以我添加了一行来设置它为false,但你仍然只能跳一次。 :/

2 个答案:

答案 0 :(得分:1)

看起来你的Sub.y需要被绑定到你的Sub.bottom所以它不会超越它。尝试:

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

答案 1 :(得分:0)

之前我做过类似的事情,我的假设是Sub.y从不等于Sub.bottom。根据y位置和垂直速度,对象的y位置永远不会完全是Sub.bottom的值。下面的代码将测试:

service.fooFunction