I have an obstacle class - Obstacle, that I want to contain subclasses of objects that spawn at different times in my project. My code is below:
Obstacle class:
Pylon class:
Having the removing and spawning within the pylons act doesn't seem to let them work, and nothing spawns. Using this method also makes it limited so that only one object will appear and disappear randomly. How would I be able to make it so that multiple objects can spawn and disappear after a certain time?
public class Obstacle extends Actor { private int spawnTimer; public void act() { } void spawning(Actor obs, int interval) { spawnTimer = (spawnTimer+1)%interval; int randX = Greenfoot.getRandomNumber(getWorld().getWidth()); int randY = Greenfoot.getRandomNumber(getWorld().getHeight()); if (spawnTimer == 0) { getWorld().addObject(obs, randX, randY); } } void removing(Actor obs, int interval) { spawnTimer = (spawnTimer+1)%interval; if (spawnTimer == 0) { getWorld().removeObject(obs); } } }
public class Pylon extends Obstacle { public void act() { Actor pyl = new Pylon(); removing(pyl, 300); spawning(pyl, 150); } }