Steampunkery Steampunkery - 1 year ago 188
Java Question

Jumping difficulty in Slick2D

I have researched how to implement a rudimentary gravity system in 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. :/

Answer Source

It looks like your Sub.y needs to be Clamped to your Sub.bottom so it doesn't go above it. Try:

if(Sub.y >= Sub.bottom) {
    Sub.y = Sub.bottom;
    sjumping = false;
    spressed = false;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download