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

2015/1/12

Keeping enemies in a maze

jreilly10 jreilly10

2015/1/12

#
Trying to keep enemies from being able leave the maze like I have done with the player but the same code wont work, help would be much appreciated.
  public boolean canMove(int x, int y)
    {

        Actor sand;
        sand=getOneObjectAtOffset(x,y,sandroad.class);

        boolean flag=false;
        if (sand !=null)
        {
            flag=true;
            setLocation(getX()+0,getY()+0); 
        }
        return flag; 
    }
 public void move ()
    {

        int random = Greenfoot.getRandomNumber(20);
        int leftChange=(-60);
        int rightChange=(60); 
        int upChange=(-60);
        int downChange=(60);
        
        {if(canMove(leftChange, 0) ==true){
                if (random == 1){
                    setLocation(getX()+leftChange, getY()) ;}
            }
        }
        {if(canMove(rightChange, 0) ==true){
                if (random == 2) {
                    setLocation(getX()+rightChange, getY()) ;}
            }
        }
        {if(canMove(upChange, 0) ==true){
                if (random == 3){
                    setLocation(getX(), getY()+upChange) ;}
            }
        }
        {if(canMove(downChange, 0) ==true){
                if (random == 4){
                    setLocation(getX(), getY()+downChange) ;}
            }
        }
    }
danpost danpost

2015/1/12

#
Lines 20 and 25 should have the 'canMove' parameters switched.
jreilly10 jreilly10

2015/1/13

#
Thank you
You need to login to post a reply.