有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Slick2D中的java跳跃难度

我研究了如何在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) 个答案

  1. # 1 楼答案

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

    if(Sub.y >= Sub.bottom) {
        Sub.y = Sub.bottom;
        sjumping = false;
        spressed = false;
    }
    
  2. # 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
    }