I need to spawn cars at two locations, one on the left side, and the other on the right side. There is two lanes, so Y coordinate varies. They, of course, need to drive to the opposite side of the screen. Please help.
That was the Car class, and here is the World class:
public class Car extends Actor { private GreenfootImage car1 = new GreenfootImage("car01.png"); private GreenfootImage car2 = new GreenfootImage("car02.png"); private GreenfootImage car3 = new GreenfootImage("car03.png"); private int speed = 4; int whichImage; int direction; public Car() { whichImage = Greenfoot.getRandomNumber(100); if(whichImage<33) { setImage("car01.png"); }else if(whichImage<66) { setImage("car02.png"); }else if(whichImage<100) { setImage("car03.png"); } } public void act() { setLocation(getX()+speed, getY()); if(isAtEdge()) { getWorld().removeObject(this); } } }
public class Road extends World { Scoreboards scoreboards = new Scoreboards(); public Road() { super(800, 600, 1); addObject(scoreboards, 75, 550); addObject(new MotherShip(), getWidth()/2, 500); addObject(new Greep(), 300, 500); addObject(new Tomato(), 100, 55); addObject(new Tomato(), 250, 112); addObject(new Tomato(), 400, 74); addObject(new Tomato(), 550, 143); addObject(new Tomato(), 700, 89); setPaintOrder(Car.class, Greep.class, Tomato.class, MotherShip.class, Scoreboards.class, Road.class); } public Scoreboards getScoreboards() { return scoreboards; } public void addACar() { if(Greenfoot.getRandomNumber(100)<50) { addObject(new Car(), 25, 265); }else { addObject(new Car(), 745, 360); } } }