I am creating an Asteroids clone, but with a few more bells and whistles.
As of right now I have an ArrayList<Asteroid> that holds all of the asteroids on screen. Each one has a Vector associated with it and extends my genereic GameObject class which handles the drawing and updating and other common things that each game object has in common.
That being said each time I destroy an asteroid I create a new Asteroid object and add it to the ArrayList<Asteroid>… There is a noticeable lag when this happens as I also create explosion particles and I assume this is the GC.
My idea was to instead of create new objects on the fly, that I can pre-create a pool of them and just re-use them.
Is this the right idea? Also what is the most organized and efficient way to go about that?
Any other ideas would be great as well. Just trying to reduce creating of all of these objects because it is definitely causing a noticeable lag. Thanks!
Creating a pool of objects and reusing them is a good idea. Also I think you could switch from
ArrayListtoVector, because Vectors are optimized for random indexation, which you’ll do a lot when using a pool.Since you say that each time you destroy an asteroid, you add a new one, it seems that you work with a constant number of asteroids. So you could create pool with a constant number of members.