Have an ArrayList that is filled with enemy objects. Then have a render method like this:
public void render(Graphics g) {
for(int i = 0; i < enemies.size(); i++) { //enemies is the arraylist
TempEnemy = enemies.get(i); // TempEnemy is a temporary enemy set to an enemy in the list so you can do method calls
TempEnemy.render(g);
}
}
You would do the same thing with the update loop.
Now you have to be able to add and remove enemies from this list so you would make methods like this:
public void addEnemy(Enemy e) {
enemies.add(e);
}
public void removeEnemy(Enemy e) {
enemies.remove(e);
}
Now in your level class or your main game class you have to make this class: (The class that has all these methods about the enemy arraylist along with the arraylist itself)
EnemyController ec = new EnemyController();
Now, to add an enemy you would just have to type:
ec.addEnemy(new Enemy(x, y));
And to remove an enemy:
ec.removeEnemy(enemy instance goes here);
Now you want to be able to render the enemies in the list on screen. So in your render loop add this:
ec.render(g); //G being a graphics object
Now you have drawn all the enemies on the screen. They won’t move if you don’t call their update method. So in you level’s update method put something like this:
ec.update();
Now, you have it. In your collision event for your player you might want to put something like this:
if(collision with enemy) {
ec.removeEnemy(enemy);
score++;
}
You would want to pass the EnemyController to the player in a constructor argument BTW.
Fell free to drop a medal! Have fun!