Hi all,
having an attempt at some programming with greenfoot, but come to a little problem. I am trying to get my code to add 3 enemies if 3 enemies are no longer present on the screen. I so far have this for the world:
and this for the enemy:
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 | public class City extends World { /** * Constructor for objects of class City. * */ public City() { super ( 700 , 300 , 1 ); populateCity(); enemySwarm(); } public void populateCity() { addObject( new Ship(), 100 , 120 ); } public void enemySwarm() { int count = 1 ; while (count < 4 ) { addObject( new enemyShip(), 800 , Greenfoot.getRandomNumber( 300 )); count++; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class enemyShip extends Enemies { private int velocity = 3 ; private int outOfBoundary = 400 ; /** * Act - do whatever the enemyShip wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { moveLeft(); if (getX() <= getWorld().getWidth() - (getWorld().getWidth() - outOfBoundary)) { getWorld().removeObject( this ); } } public void moveLeft() { setLocation(getX() - velocity, getY()); } } |