The code compiles fine, but when I click run, my object will jump to the top of the screen, and fall to about a third from the Worlds edge, where ground is placed. Any help on how to make this work correctly and look like a realistic jump is appreciated
Here is my code for the object I'm trying to make jump;
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Ghost extends Animal
{
private static final double WALKING_SPEED = 1.0;
private GreenfootImage image1;
private GreenfootImage image2;
private int speed = 7; // running speed (sideways)
private int vSpeed = 0;
private int acceleration = 2;
private int jumpStrength = 12;
public Ghost()
{
image1 = new GreenfootImage("Otis_Left.png");
image2 = new GreenfootImage("Otis_Right.png");
}
public void act()
{
controlGhost();
checkFall();
// Add your action code here.
}
public void moveBack()
{
double angle = Math.toRadians( getRotation() );
int x = (int) Math.round(getX() - Math.cos(angle) * (3 * WALKING_SPEED));
int y = (int) Math.round(getY() + Math.sin(angle) * (3 * WALKING_SPEED));
setLocation(x, y);
}
/**
* control the Ghost with arrow Keys
*/
public void controlGhost()
{
if (Greenfoot.isKeyDown("right") )
{
move();
setImage(image2);
}
if (Greenfoot.isKeyDown("left") )
{
moveBack();
setImage(image1);
}
if (Greenfoot.isKeyDown("up") )
{
jump();
}
}
public void jump()
{
vSpeed = -jumpStrength;
fall();
}
public void checkFall()
{
if(onGround())
{
vSpeed = 0;
}
else
{
fall();
}
}
public boolean onGround()
{
int myHeight = getImage().getHeight();
Actor under = getOneObjectAtOffset(0, myHeight/2, Ground.class);
return under != null;
}
public void fall()
{
setLocation ( getX(), getY() + vSpeed);
vSpeed = vSpeed + acceleration;
}
public void moveRight()
{
setLocation ( getX() + speed, getY() );
}
public void moveLeft()
{
setLocation ( getX() - speed, getY() );
}
}

