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

2014/12/10

random movement by multiples of 60

jreilly10 jreilly10

2014/12/10

#
Trying to get enemies to move randomly through a maze but i want them to move a square at a time (60 pixels)
private int random =Greenfoot.getRandomNumber(4);
 public void move ()
    {
        {
            if (random == 1);
            move (60); 
        }
        {
            if (random == 2);
            move (-60); 
        }

        {
            if (random == 3){
                setLocation(getX(), getY()+60) ;}
        }
        {
            if (random == 4){
                setLocation(getX(), getY()-60) ;}
        }
    }
danpost danpost

2014/12/10

#
As is, 'random' will always contain the same value once assigned and your actor will move in a straight line toward an edge of the world. You do not need a field for 'random'; its value does not need retained between act cycles. Remove 'private' from the line and move it to be the first line in your 'move' method.
jreilly10 jreilly10

2014/12/10

#
thanks for the help and the fast response
You need to login to post a reply.