In my game bullets are constantly created, therefore I want to use Pool
class for this. However, the problem is that I have many types of bullets. All of them extend the same parent class Projectile
. Currently there are 19 types of bullets. It's a bad idea to create a Pool
class for each of them. And more may come later.
I tried to cast BallistaArrow arrow = (BallistaArrow) world.getPool().obtain();
. However I'm getting cast exception:
[..].mygame.Projectile cannot be cast to [...].mygame.engineer.BallistaArrow
.
BallistaArrow is the child class of Projectile.
Is there any way to solve this problem, so that I can have one Pool
class for all Projectile
extending objects?
The pool contains instances of a specific type. Say it has 10 objects in it, those are going to be 10 specific Projectile
instances, you cannot decide which type the object is after you extract it from the pool.
There are two ways to solve this, at least:
Projectile
sub-types a run-time specialization, not a subclass. So you just have a Projectile
class that stores stuff common to all your bullets, and figure out the behavior difference at run-time. See Using Object Pools in Libgdx.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With