Soo... I'm trying to make an Actor stay on the ground and it doesn't work
If somebody could help me I'd be sooo grateful :D
import greenfoot.*;
public class Dolphin extends Actor
{
private int speed = 7;
private int vSpeed = 0;
private int acceleration = 2;
public void act()
{
checkKeys();
checkFall();
}
private void checkKeys()
{
if(Greenfoot.isKeyDown("left"))
{
moveLeft();
}
if(Greenfoot.isKeyDown("right"))
{
moveRight();
}
}
public void checkFall()
{
if(onGround())
{
vSpeed=0;
}
else
{
fall();
}
}
public boolean onGround()
{
Actor under = getOneObjectAtOffset ( 0, getX() / 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());
}
}
