Hi, I was wondering how I can set my actors to random locations when they hit the borders of the world
Thanks!
public void bounceAtEdges(int width,int height) // Turns when hits edge
{
int worldX = getWorld().getWidth();
int worldY = getWorld().getHeight();
if(getY()==height) //top border
{
setLocation(getX() + Greenfoot.getRandomNumber(20), getY() + Greenfoot.getRandomNumber(20));
}
if(getX()==width) //left border
{
setLocation(getX() + Greenfoot.getRandomNumber(20), getY() + Greenfoot.getRandomNumber(20));
}
if(getX()==worldX-width) //right border
{
setLocation(getX() + Greenfoot.getRandomNumber(20), getY() + Greenfoot.getRandomNumber(20));
}
if(getY()==worldY-height)//bottom border
{
setLocation(getX() + Greenfoot.getRandomNumber(20), getY() + Greenfoot.getRandomNumber(20));
}
}
