Hi,
trying to add Enemy(2) object if Enemy(2) does not exist in the world. My Enemy class has:
I've had a look at list and getObjects but it doesnt do as I need as it checks how much of Enemy.class are available. I want to check how much of just Enemy(2) object are in the World:
Any help will be much appreciated.
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Enemies within the game * * @author (your name) * @version (a version number or a date) */ public class Enemy extends Mover { private static final GreenfootImage Drone = new GreenfootImage( "Drone.png" ); private static final GreenfootImage MiniBoss = new GreenfootImage( "MiniBoss.png" ); private static final GreenfootImage Boss = new GreenfootImage( "Boss.png" ); private int droneSelection = Greenfoot.getRandomNumber( 3 ); private int life; public Enemy( int selection) { this .selection = selection; if (selection == 1 ) { setImage(Drone); this .outOfBoundary = 50 ; this .life = 1 ; } else if (selection == 2 ) { setImage(MiniBoss); this .speed = (Greenfoot.getRandomNumber( 2 ) + 2 ); } else if (selection == 3 ) { setImage(Boss); this .speed = 2 ; } } /** * Act - do whatever the Enemy wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (selection == 1 ) { switch (droneSelection) { case 1 : moveLeftPattern1(); this .speed = (Greenfoot.getRandomNumber( 3 ) + 1 ); break ; case 2 : moveLeftPattern2(); this .speed = (Greenfoot.getRandomNumber( 3 ) + 1 ); break ; default : moveLeft(); this .speed = (Greenfoot.getRandomNumber( 3 ) + 3 ); break ; } fireEnemyMissile1(); enemyBoundary(); } } /** * Remove enemy when no longer needed */ private void enemyBoundary() { if (isTouching(Hero. class )) { getWorld().addObject( new Explosion( 1 ), getX(), getY()); takeLife(); } else if (getX() <= - outOfBoundary) { getWorld().removeObject( this ); } } /** * Fire enemy missile 1 type */ private void fireEnemyMissile1() { shootingInterval++; if ( this .shootingInterval > 40 ) { this .shootingInterval = 0 ; Missiles fire = new Missiles( 2 ); getWorld().addObject(fire, getX() - 50 , getY() + 4 ); } } public void takeLife() { this .life -= 1 ; if ( this .life <= 0 ) { getWorld().removeObject( this ); } } } |
1 2 | List Enemy2 = getObjects(Enemy. class ); if (Enemy2.size() == 0 ) addObject( new Enemy( 2 ), 100 , 100 ); |