I am learning Java, LibGDX, Box2D via the use of projects and tutorials, I am following a tutorial at the moment and I wanted to raise a question regarding the render() loop and the create() method.
So first create() always gets called first, then render(). If I am creating a bunch of instances such as:
public class Game implements ApplicationListener {
World world;
Box2DDebugRenderer debugRenderer;
OrthographicCamera cam;
BodyDef bodyDef;
CircleShape circle;
FixtureDef fixtureDef;
Should I use the new
constructor here? or should I do that in the create() method?
@Override
public void create() {
world = new World(new Vector2(0, -10), true);
debugRenderer = new Box2DDebugRenderer();
cam = new OrthographicCamera(800 / 2, 480 /2);
bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(100, 300);
circle = new CircleShape();
circle.setRadius(6f);
fixtureDef = new FixtureDef();
Or should I place it in the render() method?
I am a bit unsure which is best, doing it in render seems a bit naff, would that not cause the game to constantly create new instances every loop iteration resulting in performance issues?
Oh and meant to ask, when I am defining properties of an object, such as circle.setRadius(), create method would be the right place?
You should definetely not use a lot of
new
in yourrender
loop. On desktop this isn't really a big problem, but on handheld devices this will invoke the garbage collector too often, which will result in a laggy gameplay.Whether you do it in the attribute declaration, or in
create
doesn't make a big difference. Both is done just once in your application's lifecycle.