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

2012/5/13

Actors stopping

Matt Matt

2012/5/13

#
public class Human extends Actor
{

    int offsetX = 0; //global variable storing the random X direction (1, 0 or -1)
    int offsetY = 0; //global variable storing the random Y direction (1, 0 or -1)
    
    public Human() // This sets the function for the Human
    {
        setDirection(); // This sets the direction for the Human
    }

    /**
     * Human will walk continuously in a random direction turning randomly when it hits something
     * if the Human hits a spider it will eat it.
     */
    
    public void act() // This defines the way that the Human moves (Greenfoot is in control)
    {
        int currX = getX(); //gets the currect X co-ordinate
        int currY = getY(); //gets the currect Y co-ordinate
        
        if(canMove(currX + offsetX, currY + offsetY)) //
            setLocation(currX + offsetX, currY + offsetY); //
        else
            setDirection(); //this sets the new direction of the Human
    }
    private void setDirection() //This is the function that the Human is in control of, the function defines the amount of rotation the Human does per move. 
    {
        do{
        offsetX = Greenfoot.getRandomNumber(3) - 1;// This generates a random number, it will be -1, 0 or 1. Stored in X
        offsetY = Greenfoot.getRandomNumber(3) - 1;//This generates a random number, it will be -1, 0 or 1. Stored in Y
        }while(offsetX==0 && offsetY==0);
        if(offsetX == -1 && offsetY == -1) //if the Human will go north-west
            setRotation(315); //set rotation to north-west
            
        else if(offsetX == -1 && offsetY == 0) //if the Human will go west
            setRotation(270); //set rotation to west
            
        else if(offsetX == -1 && offsetY == 1) //if the Human will go south-west
            setRotation(225); //set rotation to south west
            
        else if(offsetX == 0 && offsetY == -1) //if the Human will go north
            setRotation(0); //set rotation to north
            
        else if(offsetX == 0 && offsetY == 1) //if the Human will go south
            setRotation(180); //set rotation to south
            
        else if(offsetX == 1 && offsetY == -1) //if the Human will go north east
            setRotation(45); //set rotation to north east
            
        else if(offsetX == 1 && offsetY == 0) //if the Human will go East
            setRotation(90); //set rotation to East
            
        else if(offsetX == 1 && offsetY == 1) //if the lizard will go south east
            setRotation(135); //set rotation to South-East
    }
    private boolean canMove(int x,int y) //
    {       


        List foundTree = getWorld().getObjectsAt(x,y, Tree.class); //canMove is false if a Wall is found
        if(!foundTree.isEmpty  ())     return false;   //if wall found return false and stop otherwise continue
     
        List foundLampPost = getWorld().getObjectsAt(x,y, LampPost.class); //canMove is false if a Wall is found
        if(!foundLampPost.isEmpty  ())     return false;   //if wall found return false and stop otherwise continue
        
       List foundPurpleCar = getWorld().getObjectsAt(x,y, PurpleCar.class); //canMove is false if a Wall is found
        if(!foundPurpleCar.isEmpty  ())     return false;   //if wall found return false and stop otherwise continue
        
               List foundYellowCar = getWorld().getObjectsAt(x,y, YellowCar.class); //canMove is false if a Wall is found
        if(!foundYellowCar.isEmpty  ())     return false;   //if wall found return false and stop otherwise continue
        
                       List foundGreenCar = getWorld().getObjectsAt(x,y, GreenCar.class); //canMove is false if a Wall is found
        if(!foundGreenCar.isEmpty  ())     return false;   //if wall found return false and stop otherwise continue
        

        
        return true;    //this must be at the end - canMove will only be true if it got this far ie it didn't find any of the listed objects.
      }
        
I have programmed one of my actors to move around the world using this coding, but they are getting stuck at the edge of the world. It is as if they have been asked to stop on the edge of the world, as they move bouncing off of other actorsobjects until this point. Is their something wrong with my coding? What coding would I need to fix this issue
danpost danpost

2012/5/13

#
You do not have any checks to see if currX + offsetX or currY + offsetY are outside the limits of the world. This should probably be done in your canMove(int x, int Y) method.
Matt Matt

2012/5/14

#
        if( currX + offsetX = 0); return false;
        if( currY + offsetY = 0); return false;
I tried using this code but it doesnt work out, do you know where Im going wrong
danpost danpost

2012/5/14

#
For one thing, you need to use two equal signs to show a comparison. For another the semi-colons after the comparison gives the 'if' blocks no code; so the first 'return false;' will be acted on regardless.
if (currX + offsetX == 0) return false;
if (currY + offset == 0) return false;
if (currX + offsetX >= getWorld().getWidth() - 1) return false;
if (currY + offsetY > = getWorld().getHeight() - 1) return false;
Matt Matt

2012/5/14

#
Thanks for your help
You need to login to post a reply.