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

2014/5/16

Make Actor Stop When it Touches Another Actor

Arichman Arichman

2014/5/16

#
I have an actor called "Red_Robot"
public class Red_Robot extends Robot
{
    public void act() 
    {
       wasdDrive();
    }    
    
    public void wasdDrive()
    {       
        if (Greenfoot.isKeyDown("a"))
           turn(-3);
        if (Greenfoot.isKeyDown("d"))
            turn(3);
        if (Greenfoot.isKeyDown("w"))
            move(3);
        if (Greenfoot.isKeyDown("s"))
            move(-3);
    }
}
and I want it to not be able to pass an actor called "Horizontal_Barrier" which has no code yet. How can I make this happen. Preferably without using the stuff from the wombat tutorial scenario.
danpost danpost

2014/5/16

#
Basically, when after moving, if it is touching the barrier, move back:
public class Red_Robot extends Robot
{
    public void act() 
    {
       wasdDrive();
    }    
    
    public void wasdDrive()
    {       
        if (Greenfoot.isKeyDown("a"))
           turn(-3);
        if (Greenfoot.isKeyDown("d"))
            turn(3);
        if (Greenfoot.isKeyDown("w"))
        {
            move(3);
            if (isTouching(Horizontal_Barrier.class))
                move(-3);
        }
        if (Greenfoot.isKeyDown("s"))
        {
            move(-3);
            if (isTouching(Horizontal_Barrier.class))
                move(3);
        }
    }
}
You need to login to post a reply.