[英]Jumping difficulty in Slick2D
I have researched how to implement a rudimentary gravity system in slick2d. 我研究了如何在slick2d中实现基本重力系统。 Here is the code that I have (This is in the update function):
这是我的代码(在更新功能中):
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
}
Here is where the problem arises. 这就是问题所在。 When I press the up key, the sprite jumps and comes down normally, but pressing the up key again does nothing.
当我按下向上键时,精灵会正常跳跃并下降,但是再次按下向上键则无济于事。 I originally thought it was cause I didn't reset spressed, so I added the line to set it to false, but you can still only jump once.
我本来以为这是我没有重置spress的原因,所以我添加了将其设置为false的行,但是您仍然只能跳一次。 :/
:/
It looks like your Sub.y needs to be Clamped to your Sub.bottom so it doesn't go above it. 看起来您的Sub.y需要固定在Sub.bottom上,因此它不会超出其范围。 Try:
尝试:
if(Sub.y >= Sub.bottom) {
Sub.y = Sub.bottom;
sjumping = false;
spressed = false;
}
I've done something similar before, and my assumption here is that Sub.y never equals Sub.bottom. 我之前做过类似的事情,我的假设是Sub.y永远不等于Sub.bottom。 Depending on the y position and the vertical speed, the object's y position will never be exactly the value of Sub.bottom.
根据y位置和垂直速度,对象的y位置将永远不会恰好是Sub.bottom的值。 The code below will test for this:
下面的代码将对此进行测试:
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.