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.
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); } } }
List Enemy2 = getObjects(Enemy.class); if (Enemy2.size() == 0) addObject(new Enemy(2), 100, 100);