This site requires JavaScript, please enable it in your browser!
Greenfoot back
vegtio
vegtio wrote ...

2014/11/13

Change direction of actor when moving

vegtio vegtio

2014/11/13

#
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));
       }
    }
}
danpost danpost

2014/11/13

#
When your monster moves horizontally, it only move one pixel at a time; so, you only have to move one pixel back to get off the obstacle.
if (ground != null)
{
    setRotation(180);
    move(1);
}
should be sufficient.
vegtio vegtio

2014/11/13

#
I tryed what you said but when i started the game, it does not turn right when it touches the actor ground to the left. It goes through the actor ground.
Super_Hippo Super_Hippo

2014/11/13

#
I don't really understand the 'setRotation(180)' part. This will set the direction to the left no matter on which side you touch a ground object. You could use 'turn(180)' and move 1 then.
vegtio vegtio

2014/11/13

#
I put the setRotation so when it touches the actor "ground", to the right, my actor would turn left. What I want to do is make it so my actor would turn left when it touches a "ground" to the right, and turn right when it touches a "ground" to the left.
vegtio vegtio

2014/11/13

#
I fix my problem
public void checkObstacle()
    {
        Actor ground = getOneIntersectingObject(Ground.class);
        if(ground!=null)
        {
            move(-3);
            setRotation(getRotation()+180);
        }
  }
setRotation(getRotation()+180); was how i fixed it.
You need to login to post a reply.