im making a simple android game at the moment and was wondering how i can optimize my Enemy spawner, or if i dont have to.
At the moment, i just want to spawn enemies, more as the level goes on. The Game is nothin fancy, just one screen with – i would guess – max. 20 at a time on it.
So my approach is:
ArrayList<Enemy> enemies = new ArrayList();
I have an ArrayList where i will store all my spawned enemies (from the same class).
Now in the main update function i have the following:
for (Enemy en : enemies){
en.update();
}
This would be for the position-update, now i maybe have to run a second time through the cycle to determine wich of the enemies can be deleted, because it is destroyed.
Is this the most common and effective way for a simple game?
Try adding a property to the enemy called “dead” or “disposed”.
So you can keep track of which enemies needs to be released doing:
You can add effects to if you don’t want it to simply “dissapear” from the screen:
of course you’ll need to put this logic inside the update of enemy class. This is just an idea.