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

2012/5/13

Changing speed of an actor :)

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 am creating a parking game and have humans running around the car park. I am happy with the speed of the game as a whole, but want the humans to be moving slower. How would I go aboout doing this, what coding would I need?
danpost danpost

2012/5/13

#
You can move everything else faster (more cells per act) and decrease the speed of the scenario itself to compensate.
danpost danpost

2012/5/13

#
Your setDirection() code can be reduced to:
private void setDirection()
{
    int direction = Greenfoot.getRandomNumber(8);
    offsetX = (direction == 0 || direction == 4) ? 0 : Math.signum((direction + 4) % 8 - 4);
    offsetY = (direction == 2 || direction == 6) ? 0 : -Math.signum((direction + 2) % 8 - 4);
    setRotation( direction * 45);
}
You need to login to post a reply.