Hi.. I need help with trying to get my object (a man) to jump.. Right now I have the code to make him jump, but this makes him fall to the bottom of the world and jump from there. I want him to be able to jump from whichever point he is at in the world.
Here is the code I have so far:
public void act()
{
int groundLevel = getWorld().getHeight() - getImage().getHeight()/2;
boolean onGround = (getY() == groundLevel);
if (!onGround) // in middle of jump
{
ySpeed++; // adds gravity effect
setLocation(getX(), getY()+ySpeed); // fall (rising slower or falling faster)
if (getY()>=groundLevel) // has landed (reached ground level)
{
setLocation(getX(), groundLevel); // set on ground
Greenfoot.getKey(); // clears any key pressed during jump
}
}
else // on ground
{
if ("space".equals(Greenfoot.getKey())) // jump key detected
{
ySpeed = -15; // add jump speed
setLocation(getX(), getY()+ySpeed); // leave ground
}
}
}
