thanks for your time!

I'm really new to LibGDX (and Java itself).

Which approach should I use to render my different game parts? I want to show a background, the main character, a bunch of enemies and an HUD. The playable area will be bigger than the screen so the camera has to move following the main character (top view). Also everything has to be procedurally generated.

For example, I have the code to generate the map (background) but I don't know how to render it if it's in another class (not the core entry point one). I can think of calling a custom background.draw() method from my main.render() class and pass a batch to draw onto but probably there are better ways. I've tried to use Stages (scene2D) aswell but I can't get my camera to move (are they mostly made for UI?).

I'd like to be pointed towards the best solution performance-wise since it's gonna be called every frame :P

  • \$\begingroup\$ Scene2D is probably your best option. Check this out. Moving the camera is very simple. Just do this: stage.getCamera().position.set(newX, newY, newZ); stage.getCamera().update(); \$\endgroup\$ Commented Jun 15, 2015 at 16:49

3 Answers 3


just send a reference to the other class with the main class.

like this:

Otherclass(myGame game) {... code}

Now you can access render here.


Using SpriteBatch is likely going to be the best option. Passing your batch as an argument to another class' method is generally not going to have any noticeable performance impact. This is also a very easy way of going about the problem because you can just call batch.draw() on your textures inside the desired method.

  • \$\begingroup\$ Another little question. Should I extend/implement a specific class in my background or I can keep it as it is? \$\endgroup\$
    – Fiochkij
    Commented Jun 15, 2015 at 12:13
  • \$\begingroup\$ @AndreaFiocchi it's your code. Do whatever you like. Just be sure you successfully avoid Blob antipattern. \$\endgroup\$ Commented Jun 15, 2015 at 12:25
  • \$\begingroup\$ I agree with @nikoliazekter. As long as the code is organized and works for you, it's good! \$\endgroup\$
    – Alex
    Commented Jun 15, 2015 at 12:42

To render in a seperate class create a Renderer.java file with a single method named render and create an instance of it in the create() method of your main game class called Renderer renderManager = new Renderer() (filling in any constructors you need).

In the render() method of your main game class call renderManager.render(). Now any code that would normally go in the main render method can be placed in the render method of your Renderer. It will be executed as it would if it were in your main class.

As for HUDS, well, I have never had need to use them, so I have no clue. This might be of use to you. You can probably find a few tutorials online with a simple Google search and I know there are multiple StackOverflow pages on it. Check out the api docs also.

This tutorial on the libGdx wiki teaches you how to use the Orthographic camera to use world coordinates instead of pixels and to avoid the headache that comes with using pixels. Since it is described so well on the linked page I will not even attempt to explain it better.


To create a HUD, you can use a seperate camera and overlay it. Do your game drawing first, then your HUD drawing. You could alternatively create a second SpriteBatch and not set the projection matrix to that of the camera. See below:

public void create()
    OrthographicCamera cam = new OrthographicCamera();

    SpriteBatch gameBatch = new SpriteBatch();
    SpriteBatch hudBatch = new SpriteBatch();

public void render()
    //Draw using camera matrix

    //Draw using the hud

You could alternatively use the camera.project(Vector3 coords) to draw to screen coordinates what you want. This will achieve the same results.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .