【问题标题】:Jumping difficulty in Slick2DSlick2D 中的跳跃难度
【发布时间】:2016-07-07 18:25:23
【问题描述】:

我研究了如何在 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,但是你还是只能跳一次。 :/

【问题讨论】:

    标签: java slick2d gravity


    【解决方案1】:

    看起来你的 Sub.y 需要被夹在你的 Sub.bottom 上,这样它就不会超过它。试试:

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

    【讨论】:

    • 谢谢!!但对于阅读本文的人来说,它必须是 >=,而不是
    • 是的,没有意识到你的底值更大。我做了适当的编辑。也请标记为答案:)
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多