1
votes

I'm creating a Tower Defense game and since I expect to be making a lot of projectiles I thought I would Pool them. However, my projectiles have Body fields and Fixtures for collision detecting, and when I call free() on the projectiles their bodies remain in the world.

So I tried to destroy the Bodies using world.destroyBody() but that was causing null pointer errors unless I explicitly did it outside of the world.step by flagging my objects like I learned here. But then when I tried implementing that method for destroying the bodies, there were errors because the body being destroyed wasn't matching up with when the projectile was being freed.

What is the best way to go about managing memory with Poolable objects and Bodies?

1

1 Answers

1
votes

Instead of deleting the bodies and recreating them you could just store them far off screen by overriding the reset method of your poolable object.

@Override
public void reset(){
    body.setTransform(new Vector2(-50,-50), 0);
    body.setLinearVelocity(0, 0);
    body.setActive(false);
}