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

2012/12/5

Sharing variables between classes.

Jrm1715 Jrm1715

2012/12/5

#
I am trying to share variables between my Road class and my Car class. I tried accomplishing this by creating a new instance like this:
public Car(Counter pointCounter)
    {        
        Road road = new Road();
        int copCount = road.numCops;
and here is my Road class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;

/**
 * Write a description of class Road here.
 * 
 * @author (Jason Mattis) 
 * @version (12/5/2012)
 */
public class Road extends World
{     
    private static final int SPAWN_TIME = 500;
    private int copSpawnCount;
    
    public int numCops;
    
    /**
     * Act - do whatever the World wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        checkCop();
    }    
    
    /**
     * Constructor for objects of class Road.
     * 
     */
    public Road()
    {    
        super(700, 700, 1);         
        prepare();        
        copSpawnCount = SPAWN_TIME; 
        
    }
    
    /**
     * Checks to see how many Cops are in the world. 
     */
    private void checkCop()
    {      
        numCops = getObjects(Cop.class).size();
        if (numCops < 2)
        {
            copSpawnTimer();            
        }
    }
    
    /**
     * A counter that delays the spawning of a new Cop.
     */
    private void copSpawnTimer()
    {
        if (copSpawnCount > 0)
        {
            copSpawnCount--;
            if (copSpawnCount == 0) {
                createNewCop();
            }
        }        
    }   
    
    /**
     * Creates a new Cop at random x, y coordinates. 
     */
    private void createNewCop()
    {        
        if ( numCops < 2 )
        {
            
            Cop newCop;
            newCop = new Cop();
            
            World world;
            
            
            int worldWidth = getWidth();
            int worldHeight = getHeight();
            
            int x = Greenfoot.getRandomNumber(worldWidth);
            int y = Greenfoot.getRandomNumber(worldHeight);
            
            addObject(newCop, x, y);
            
            numCops = getObjects(Cop.class).size(); 
            
        }        
        copSpawnCount = SPAWN_TIME;       
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {       
        Counter counter = new Counter();
        addObject (counter, 52, 23);
        Pedestrian pedestrian = new Pedestrian();
        addObject(pedestrian, 236, 596);
        Pedestrian pedestrian2 = new Pedestrian();
        addObject(pedestrian2, 551, 639);
        Pedestrian pedestrian3 = new Pedestrian();
        addObject(pedestrian3, 275, 75);
        Pedestrian pedestrian4 = new Pedestrian();
        addObject(pedestrian4, 561, 91);
        Pedestrian pedestrian5 = new Pedestrian();
        addObject(pedestrian5, 96, 97);
        Pedestrian pedestrian6 = new Pedestrian();
        addObject(pedestrian6, 439, 242);
        Pedestrian pedestrian7 = new Pedestrian();
        addObject(pedestrian7, 618, 453);
        Pedestrian pedestrian8 = new Pedestrian();
        addObject(pedestrian8, 401, 639);
        Pedestrian pedestrian9 = new Pedestrian();
        addObject(pedestrian9, 439, 442);
        Pedestrian pedestrian10 = new Pedestrian();
        addObject(pedestrian10, 90, 630);
        Pedestrian pedestrian11 = new Pedestrian();
        addObject(pedestrian11, 179, 297);
        Pedestrian pedestrian12 = new Pedestrian();
        addObject(pedestrian12, 590, 256);
        pedestrian.setLocation(243, 550);
        Cop cop = new Cop();
        addObject(cop, 280, 673);
        Cop cop2 = new Cop();
        addObject(cop2, 418, 74);
        Car car = new Car(counter);
        addObject(car, 78, 411);
        addObject(new Instruct("Press 'W' to go forward and 'D' and 'A' to turn right and left."), 350, 350); 
        
    }    
}
What I want to do with this variable is when a cop is killed, I want to add points to the counter. When I implemented the above code it compiled just fine. But the scenario is blank after I compile everything. I appreciate any help I can get!
danpost danpost

2012/12/5

#
First, remove line 76 in the Road world class code, as you are not using the variable anywhere. Now, with your issue: your first statement in the Car class constructor will create a new Road world that is a seperate world, apart from the Road world that is currently active in your scenario. As the Car object that is being created is not yet in the world, you will not be able to get a reference to the active world until the Car object is added to the world (or you also pass a reference to the active Road world as a parameter in the Car constructor -- ). I do not know what you intend for your scenario; but, I would like to know if it is really neccessary for the number of cops in the world to be known in the Car class. In fact, I would like to know why you need to keep track of that number at all (as 'getObjects(Cop.class).size()' does return that value -- or in an actor class with the actor already in the world 'getWorld().getObjects(Cop.class).size()' ).
danpost danpost

2012/12/5

#
You could, in the Road world class, get rid of all references to 'numCops' and do the following
// in the act method, you already have the following line
checkCops();
// replace lines 39 through 91 with the following single method
private void checkCops()
{ 
    if (copSpawnCount>0) copSpawnCount--;
    if (copSpawnCount>0 || getObjects(Cop.class().size()>1) return;
    int x = Greenfoot.getRandomNumber(getWidth());
    int y = Greenfoot.getRandomNumber(getHeight());
    addObject(new Cop(), x, y);
    copSpawnCount = SPAWN_TIME;
}
With this simple straight-forward method, you are controlling the timer and adding cops when neccessary.
Jrm1715 Jrm1715

2012/12/6

#
The reason I wanted to keep track of the number of Cops in my world is for when a Cop is removed from the world (gets destroyed within the game) and after a specific amount of time it another Cop is created within the world (My scenario: Scenario ). I asked for help and was given this in response :
int numCops = getObjects(Cop.class).size(); 
Using this I came up with a way for when the Cop is destroyed, a timer will start counting down and once that timer gets to zero it will create a new Cop. I knew there was probably a more efficient way of accomplishing this, but that is what I came up with on my own.
Jrm1715 Jrm1715

2012/12/6

#
But I see how your example is much neater and works exactly the same. I was having trouble coming up with a cleaner all-in one method but had difficulty.
You need to login to post a reply.