I am trying to get my actor to change its direction when it touches a different actor. I want my actor to turn left when it touches a different actor to the right, and if it touches a actor to the left I want it to turn right. I am trying to do that in my checkObstacle method, but its not working.
public class Monster extends Actor
{
private GreenfootImage image1;
private GreenfootImage image2;
private int vSpeed = 0;
private int acceleration = 2;
/**
* Act - do whatever the Monster wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public Monster()
{
image1 = new GreenfootImage("monster.png");
image2 = new GreenfootImage("monster1.png");
setImage(image1);
}
/**
* Method act
*
*/
public void act()
{
checkFall();
move();
}
public void checkFall()
{
if(onGround()){
vSpeed = 0;
}
else{
fall();
}
}
public boolean onGround()
{
Actor under = getOneObjectAtOffset(0, getImage().getHeight()/2, null);
return under !=null;
}
public void fall()
{
setLocation ( getX(), getY() + vSpeed);
vSpeed = vSpeed + acceleration;
}
public void checkObstacle()
{
Actor ground = getOneIntersectingObject(Ground.class);
if(ground!=null)
{
move(-50);
setRotation(180);
}
}
public void move()
{
checkObstacle();
if (Greenfoot.getRandomNumber(100) < 50)
{
move(Greenfoot.getRandomNumber(2));
}
}
}

