This is what I do in Cosmic Trip:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public void update(float deltaTime) { for (int i = 0; i < maxActors; i++) { if (actor[i] != null) { if (actor[i].isAlive()) { actor[i].update(deltaTime); } else if (!actor[i].isLockedInUniverse()) { actor[i] = null; actors--; } } } sortActors(); checkCollisions(); } |
As you can see, I just 'null' the reference of most Actors when they die and let the GC do it's job, except the Actors with isLockedInUniverse() = true (things like bullets, things that get respawned very often) although I probably didn't need to worry about them either.
You'll also see that I use a raw array for keeping track of the Actors. I once moved to ArrayList, but it was a lot slower (it spent *ages* in ArrayList.RangeCheck() ), so I changed it back.
I should still implement my own List without range checking everywhere, but didn't get around to it yet.