Friday, 6 September 2013

Create() or Render() for creating instances

Create() or Render() for creating instances

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?

No comments:

Post a Comment