Hello, I am trying to make this player jump only once per key press. I find that if I hold down the key (space key), it jumps over and over. I want it to jump once and fall regardless if I let go of the key. I am thinking I need to add another if-statement but not sure how to code. Your help is appreciated. Thank you...
public class Willa extends AnimatedActor
{
private int vSpeed = 0;
private int acceleration = 1;
private int jumpHeight = -12; // how high to jump
/**
* Act - do whatever the Willa wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
control();
checkFalling();
}
private void control()
{
if (Greenfoot.isKeyDown("right"))
{
animate (4,5);
}
if (Greenfoot.isKeyDown("left"))
{
animate (2,3);
}
if (Greenfoot.isKeyDown("space")&& (onGround() == true))
{
vSpeed = jumpHeight;
fall();
}
}
private void fall()
{
setLocation(getX(),getY()+ vSpeed);
vSpeed = vSpeed + acceleration;
}
private boolean onGround()
{
Actor under = getOneObjectAtOffset(0,getImage().getHeight()/2,Ground.class);
return under != null;
}
private void checkFalling()
{
if (onGround() == false)
{
fall();
}
}
}
