Java – libgdx object pool for many objects of the same parent class

In my game, bullets are constantly created, so I want to use the pool class But the problem is that I have many types of bullets All of these extend the same parent class, projectile There are currently 19 seed bombs Creating a pool class for everyone is a bad idea More are likely to come

I tried to cast ballistaarrow = (ballistaarrow) world getPool(). Get () But I got a spell exception:

[..]. mygame. Projectile cannot project to [...] mygame. engineer. BallistaArrow.

Ballistaarrow is a children's class of projectile

Is there a way to solve this problem, so I can provide a pool class for all projectile extension objects?

Solution

A pool contains instances of a specific type Suppose it has 10 objects, and those will be 10 specific projectile instances. After extracting objects from the pool, the object type cannot be determined

There are two ways to solve this problem, at least:

>Multiple pools are used, one for each bullet type You can place a wrapper around the pool according to the typing parameters to know which pool to use This may not be the disadvantage of the solution Empty pools are not a big problem If a certain type of projectile is used for a period of time, you may encounter retention problems and then do not use it (its swimming pool is still full). > Make your projectile subtype a runtime specialization, not a subclass So you just need a projectile class to store what all bullets have in common and find out the behavior differences at run time See using object pools in libgdx

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
分享
二维码
< <上一篇
下一篇>>