I am trying to create a randomly moving enemy with a constant speed, but random direction and a limit to how far it can move along the y axis. It's very choppy and not smooth at all. here is the code below, please help!!
int timer = 0;
public void act()
{
timer ++;
if (timer == 20)
{
move();
timer = 0;
}
}
public void move()
{
if (RLevelOne.class.isInstance(getWorld()))
{
if(atLeftEdge())
{
setLocation(getX()+10,getY());
}
if(atRightEdge())
{
setLocation(getX()-10,getY());
}
if(atUpEdge())
{
setLocation(getX(),getY()+10);
}
if(atDownEdge())
{
setLocation(getX()+10,getY()-10);
}
setLocation(getX() + Greenfoot.getRandomNumber(10),getY() + Greenfoot.getRandomNumber(10));
}
}
private boolean atLeftEdge()
{
return (getX()>=getWorld().getWidth()-5);
}
private boolean atRightEdge()
{
return (getX()<= 5);
}
private boolean atUpEdge()
{
return (getY()<=5);
}
private boolean atDownEdge()
{
return (getY()>=getWorld().getHeight()/2);
}
