I use a ‘manager’ class which spawns the bad guys - this manager class has a thread safe collection list in there where the bad guys are stored.
// Store baddies in here
private CopyOnWriteArrayList<BadGuy> badGuys;
Later in my code in the manager class I have a render which calls the render method on the bad guys:
public void renderBadGuys(Camera camera, SpriteBatch batch) {
for (int i = 0; i < badGuys.size(); i++) {
BadGuy badguy = badGuys.get(i);
if(badguy!=null)
{
if (badguy.health <= 0) // if bad guy dead, remove them from the
// list
removeBadGuy(badguy);
else {
badguy.move(badguy.x, badguy.y, camera);
// Only draw outside baddies that are in the camera's view
if ((badguy.x >= camera.position.x - (width + 30) && badguy.x <= camera.position.x + width + 10)
&& (badguy.y >= camera.position.y - height && badguy.y <= camera.position.y + height)) {
badguy.render(batch);
}
}
}
}
}
If I was you, I’d have an abstract parent class of BadGuy and derive from this with your other ‘bad guys’, this abstract class would have various abstract
methods in there such as move, attack and by using polymorphism like this will allow your other bad guys to act differently if you know what I mean?!