I'm starting to create my World with Box2d in Libgdx and I have to create shapes for different game objects. The tutorial I've read said I should dispose my shapes when I'm done using them.
So, I started keeping references like that:
private CircleShape circle;
private PolygonShape ground;
private PolygonShape wall;
private PolygonShape box;
//...
//(getters)
And disposing my objects like that:
@Override
public void dispose()
{
circle.dispose();
ground.dispose();
wall.dispose();
box.dispose();
world.dispose();
}
I decided to change this to a list for expansion but the problem is somewhere else in my code, I'm adding bodies on click so I need to let the access to some shapes from external classes. I could create extra shapes and let access to my list but I don't like the idea of creating a giant list of disposable objects.
A solution would be to create a ShapeManager object that has an internal list of shapes. I could dispose this object and it would wrap the shapes constructors letting me return an already existing shape if it fit the need.
However, this solution seem too heavy. Why Box2d (or LibGDX) made the shapes objects that need to be disposed ? Is there a class like I described already included in LibGDX ? Is there a better solution ?
You can dispose after making the definition of your body.