So in my 2D game, you can just hold up and the character will jump as soon as he hit the ground.
It looks like the character is bouncing :/
What i want to achieve is to make it behave like any other 2D game, if you hold up and land, you wont jump again until you repress the jump button.
This is a simple thing I want to achieve, but hard as hell to implement, as I my game have wall jumping and short-jumps.
Here is the code, I will hide unnecessary code to avoid confusion:
boolean upInput = in.isKeyDown(KEY_UP); //If the jump button is pressed
if(cant move down && vy <= 0)
reset();
else
{
vy *= 1.0 - (damping * DELTA);
float force = mass * gravity;
vy += (force / mass) * DELTA;
}
if(jumpAllowed && upInput)
{
if(++counter > maxY)
{
jumpAllowed = false;
counter = 0;
}
else
vy = upSpeed;
}
else
jumpAllowed = false;
boolean falling = vy < 0;
float nextY = currY - vy * DELTA;
if(vy != 0)
{
/**
* Do not change these codes!
* It allow us to jump up, fall down and run down from slopes!!!
*/
if(!falling)
{
if(canGoToUp(nextY))
moveTo(currX, nextY);
else
vy = 0; //If we jump into an ceiling, we start falling down.
}
else if(falling && canGoToDown(nextY))
moveTo(currX, nextY);
else
{
tryDown(10);
jumpAllowed = true;
}
}
protected void reset()
{
jumpAllowed = true;
vy = counter = 0;
}
The jumping occur in if(jumpAllowed && upInput).