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

2012/11/29

Need help with getting the amount of objects in the world.

Jrm1715 Jrm1715

2012/11/29

#
I am trying to find a way to get the number of Cops that are in the world and then store in a variable as an integer. My reason for this is that I want to be able to have a specific number of Cops in my world and when one or more cops is destroyed, one will be created in my world. here is my Cop class.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Cop here.
 * 
 * @author (Jason Mattis) 
 * @version (10/13/2012)
 */
public class Cop extends Vehicle
{
    private static final int ANIMATE_DELAY = 10;
    private static final int TURN_DELAY = 50;
    private int copDelayCount;
    private int turnDelayCount;
    
    

    private GreenfootImage image1;
    private GreenfootImage image2;
    /**
     * Act - do whatever the Cop wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(5);
        turnAtEdge();
        eatCar();
        checkAnimatedCop();
        randomTurnDelay();       
    }   
        
    /**
     * initiates the timer.
     */
    private void animatedDelayAmount()
    {
        copDelayCount = ANIMATE_DELAY;
    }

    /**
     * Checks to see if the timer is greater than zero. If this is true, it decrements the count
     * by one. When the count reaches zero, the animateCop method is called. 
     */
    private void checkAnimatedCop()
    {
        if ( copDelayCount > 0)
        {
            copDelayCount--; 
            if (copDelayCount == 0){
                animateCop();
            }
        }
    }

    /**
     * Alternates two cop car images to create an animation of the lights flashing. 
     */
    public void animateCop()
    {
        if ( getImage() == image1 )

        {
            setImage(image2);
        }
        else
        {
            setImage(image1);
        }
        animatedDelayAmount(); //resets count to default
    }

    /**
     * Create a Cop and initialize its two images.
     */
    public Cop()
    {
        image1 = new GreenfootImage("Cop.png");
        image2 = new GreenfootImage("Cop2.png");
        setImage(image1);
        copDelayCount = 0;
        animatedDelayAmount();
        turnCopDelayCount();

    }
    
    /**
     * Check to see if the Car is at the edge of the map. If
     * it is, then it turns around 17 degrees.
     * 
     */
    public void turnAtEdge()
    {
        if ( atWorldEdge() )
        {
            turn(17);
        }
    }

    private void turnCopDelayCount()
    {
        turnDelayCount = TURN_DELAY;
    }

    private void randomTurnDelay()
    {
        if (turnDelayCount > 0)
        {
            turnDelayCount--;
            if (turnDelayCount == 0){
                randomMovement();
            }
        }
    }

    /**
     * Makes the Cop car move left or right randomly 45 degrees. The chance of 
     * the Cop move left or right is chosen randomly out of 100. If the number
     * is less than 10, it will execute that turn. 
     */
    public void randomMovement()
    {
        turn( Greenfoot.getRandomNumber(180) - 90);
        turnCopDelayCount();
    }
    
    /**
     * Allows the Cop to go to the edge of the world and appear on the oppisite side. 
     */
    public void setLocation(int x, int y)
    {
        int width = getWorld().getWidth();
        int height = getWorld().getHeight();
        
        while (x >= width)
        {
            x -= width;
        }
        while (x < 0)
        {
            x += width;
        }
        while (y >= height)
        {
            y -= height;
        }
        while (y < 0)
        {
            y += height;
        }
        
        super.setLocation(x, y);
    }

    /**
     * Checks whether the Cop is at a Car. If the Cop is at one, it 
     * pulls the Car over.
     */
    public void eatCar()
    {
        if ( canSee(Car.class) )
        {
            eat(Car.class);
            Greenfoot.stop();
            Greenfoot.playSound("short-siren.mp3");
        }
    }
}
I tried using the getObjects() method, but it did not work because it needed to be an integer.
SPower SPower

2012/11/29

#
int numCops = getObjects(Cop.class).size();
Jrm1715 Jrm1715

2012/12/5

#
After a little tinkering it worked. Thanks for the help!
You need to login to post a reply.