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!
1 2 3 4 | public Car(Counter pointCounter) { Road road = new Road(); int copCount = road.numCops; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | 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 ); } } |