Hi all,
I am trying to make my World spawn enemies every 100 act cycles how can I do that?
//in the active world private int spawnFrequency = 100, spawnTimer = spawnFrequency; public void act() { if (--spawnTimer == 0) { spawnTimer = spawnFrequency; addObject(new Enemy(), 50, 50); } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class Background extends World { public static final int WIDE = 1182; public static final int HIGH = 432; private Scroller scroller; private int spawnFrequency = 100, spawnTimer = spawnFrequency; private int enemyClass = Greenfoot.getRandomNumber(5); public Background() { super(WIDE,HIGH,1,false); GreenfootImage image = new GreenfootImage("Background.png"); scroller = new Scroller(this,image); prepare(); } public void act() { scroll(); } private void scroll() { int speed = 2; scroller.scroll(speed, 0); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { Trex trex = new Trex(); addObject(trex,165,292); trex.setLocation(140,302); trex.setLocation(138,294); trex.setLocation(107,69); if (--spawnTimer == 0) { spawnTimer = spawnFrequency; if (enemyClass == 1) { addObject(new threeShort(), 1077, 325); } else if (enemyClass == 2) { addObject(new bird(), 1077, 325); } } } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class Background extends World { public static final int WIDE = 1182; public static final int HIGH = 432; private Scroller scroller; private int spawnFrequency = 100, spawnTimer = spawnFrequency; private int enemyClass = Greenfoot.getRandomNumber(5); private int birdHeight = Greenfoot.getRandomNumber(3); public Background() { super(WIDE,HIGH,1,false); GreenfootImage image = new GreenfootImage("Background.png"); scroller = new Scroller(this,image); prepare(); } public void act() { scroll(); if (--spawnTimer == 0) { spawnTimer = spawnFrequency; if (enemyClass == 1) { addObject(new threeShort(), 1077, 325); } else if (enemyClass == 2) { if (birdHeight == 1) { addObject(new bird(), 1077, 134); } else if (birdHeight == 2) { addObject(new bird(), 1077, 213); } else if (birdHeight == 3) { addObject(new bird(), 1077, 292); } } else if (enemyClass == 3) { addObject(new one(), 1077, 309); } else if (enemyClass == 4) { addObject(new threeTall(), 1077, 313); } else if (enemyClass == 5) { addObject(new two(), 10777, 310); } } } private void scroll() { int speed = 2; scroller.scroll(speed, 0); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { Trex trex = new Trex(); addObject(trex,165,292); trex.setLocation(140,302); trex.setLocation(138,294); trex.setLocation(107,69); } }