Java – how to improve efficiency?

I'm creating an asteroid clone, but there are still some bells and whistles

So far, I have an ArrayList < < asteroid >, which saves all asteroids on the screen Each has a vector associated with it and extends my generic GameObject class, which handles drawing and updating, as well as other common things common to each game object

It is said that whenever I destroy an asteroid, I will create a new asteroid object and add it to ArrayList < < asteroid >... There will be a significant delay when this happens, because I will also create explosive particles. I think this is GC

My idea is not to create new objects dynamically. I can create their pools in advance and reuse them

Is that the right idea? So what is the most organized and effective method?

Any other idea would be great Just try to reduce the creation of all these objects, because it will certainly lead to significant lag thank you!

Solution

It's a good idea to create a pool of objects and reuse them In addition, I think you can switch from ArrayList to vector, because vector is optimized for random index, and you will do a lot of things when using pool

Since you say that every time you destroy an asteroid, you will add a new asteroid, it seems that you have used a constant number of asteroids Therefore, you can create a pool with a constant number of members

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>