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

2021/3/14

Turn if hit wall does not work

gfsdhjjks gfsdhjjks

2021/3/14

#
My problem is that as soon as I hit a wall, my object just stops moving but it does not turn around.
import greenfoot.*;
public class enemy extends Actor
{
    int i;
    public void act() 
    {
        moveAround();
    }
       public boolean hitWall()
    {
        if (isTouching (walls.class))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public void moveAround()
    {
        move(3);
        if(isTouching (walls.class)==true)
        {
            move(-3);
            int i = Greenfoot.getRandomNumber(3);
        }
        if(i==1)
        {
            setRotation(90);
            i=0;
        }
        if(i==2)
        {   
            setRotation(-90);
            i=0;
        }
    }
}
danpost danpost

2021/3/14

#
Shift lines 28 thru 37 up one line (to immediately after line 26). You can also remove line 4.
gfsdhjjks gfsdhjjks

2021/3/15

#
danpost wrote...
Shift lines 28 thru 37 up one line (to immediately after line 26). You can also remove line 4.
thanks for ur help
You need to login to post a reply.