Hey, I'm trying to figure out how to make my actor jump higher or lower, depending on how long you hold the jump button, like in the Mario games. I have jump code, but it only jumps a specific height. Thanks for anyone who can help.
/**
* Tells the Player what to do
*/
public void act()
{
proccessKeys();
checkFall();
}
/**
* Sees which keys are pressed, and does an according action.
* myStats is a GameStats object that contains the keys for moving left,
* right, and for jumping. These can be changed later. Default is "a", "w", "d"
*/
public void proccessKeys()
{
if (Greenfoot.isKeyDown(myStats.left))
moveLeft();
if (Greenfoot.isKeyDown(myStats.right))
moveRight();
if (Greenfoot.isKeyDown(myStats.up) && onGround())
jump();
}
/**
* Makes the Player jump
*/
public void jump()
{
gravity = -15;
}
/**
* Checks if the player is in the air, if so, the player must fall.
*/
public void checkFall()
{
if (!onGround() || gravity < 0)
fall();
}
/**
* Moves the Player down
*/
public void fall()
{
if (gravity < -15)
gravity = -15;
//MAX_VELOCITY = 5. So it doesn't fall through ground
if (gravity < MAX_VELOCITY)
gravity++;
setLocation(getX(), getY() + gravity);
}
/**
* Checks if the player is on the ground.
*/
public boolean onGround()
{
int height = getImage().getHeight();
Actor ground = getOneObjectAtOffset(0, height/2, Ground.class);
if(ground != null)
return true;
return false;
}