Okay, I'm trying to create a simulation where the character has to avoid these falling rocks. The problem is, the rocks keep multiplying. They're supposed to fall one at a time. Well, even two and four at a time would be fine. But, they multiply to the point that it creates a line of rocks falling from the sky.
Here's the complete rock code:
And the complete world code:
It works when compiled, but I can't seem to fix this one issue. Please help!
public class Rock extends Antagonist { private static final int EAST = 0; private static final int WEST = 1; private static final int NORTH = 2; private static final int SOUTH = 3; private int direction; private int rocksPresent; private int timer; public Rock() { timer = 100; rocksPresent = 0; } public void act() { setDirection(3); move(5); checkTimer(); findRocks(); checkLocation(); } public void setDirection(int direction) { if ((direction >= 0) && (direction <= 3)) { this.direction = direction; } switch(direction) { case SOUTH : setRotation(90); break; case EAST : setRotation(0); break; case NORTH : setRotation(270); break; case WEST : setRotation(0); break; default : break; } } public void checkTimer() { if (timer > 0) { timer --; } else { getWorld().addObject(new Rock(), Greenfoot.getRandomNumber(560), 0); timer =100; rocksPresent=rocksPresent+1; } } public void findRocks() { if (rocksPresent >=4) { Greenfoot.stop(); } } public void checkLocation() { if (getY() == 283) { getWorld().removeObject(this); } } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class PlanetEarth here. * * @author (your name) * @version (a version number or a date) */ public class PlanetEarth extends World { private int timer; /** * Constructor for objects of class PlanetEarth. * */ public PlanetEarth() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); prepare(); populateWorld(); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { YOU you = new YOU(); addObject(you, 54, 283); you.setLocation(47, 278); you.setLocation(301, 280); you.setLocation(304, 278); } public void populateWorld() { addObject(new Rock(), Greenfoot.getRandomNumber(560), 0); } }