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:
and here is my Road class:
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!
public Car(Counter pointCounter) { Road road = new Road(); int copCount = road.numCops;
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); } }