LibGDX: Scene2d – Actors Part 1

Scene2d is a higher level framework for creating games. It’s not mandatory to use it but sometimes it is very useful because code is more friendly and simpler. It is indicated for menu, buttons, checkboxes etc. but it’s useful for board games, character’s equipment and more.

Scene2d is composed of 3 classes regarding Scene2d :

  1. Actor: The Actor class is a node in the graph which has a position, rectangular size, origin, scale, rotation, and color.
  2. Group: The Group class is an actor that may have child actors.
  3. Stage: The Stage class has a camera, SpriteBatch, and a root group and handles drawing the actors and distributing input events.

Stage is a container that has and handles actors. Also it has act method that takes a delta time as an argument and it calls act method of every Actor, allowing the actors to take some action based on time. As you can see act method is something like the update method that we already know.

Constructor of Stage class accepts only 2 arguments, Viewport and Batch. It doesn’t need to pass camera because Viewport handles camera internally. Batch argument is optional as well. The Stage class handles batch internally.

So, it doesn’t need to dispose of Batch manually because stage will. That’s only in the case that we let Stage object to handle Batch alone. On the contrary, if we pass Batch as an argument in Stage then we have to dispose of it manually. As a rule, it’s better to let Stage object to handle batch alone.

Also, it doesn’t need to use setProjectionMatrix or batch.begin() or batch.end() manually because Stage handles these automatically.

As a result, we have a more simple and maintainable code.

Sample Code

We will see an example with Actors. We will start with class MyActor which “extends” Actor.

Constructor of MyActor class takes 2 arguments, a Texture and a String. Also, in constructor, we set an event using addListener for our Actor. In case that Actor is touchable, we have to set it with the following line.

A very important notice is to call setBounds() method with the right arguments.

In this example, spritePos() is where sprite position is changed and setBounds() method is called.

Next step is to pass our actors in Stage. Take a look at the following code.

As you can see, we have to pass Viewport as argument in Stage and actors using addActor() method.

If you don’t know how to use AssetManager & AssetDescriptors take a look in LibGDX: AssetManager


In render() method:

Act method isn’t needed in our case because we don’t use Actions in this example.

If we run the code we will see the following output:


Clicking each asset, we will see in console the following output:

Touch down asset with name : images/character.png
Touch down asset with name : images/null.png
Touch down asset with name : images/skeleton.png

Stage can handle events right and when we touch down any of assets an event is triggered.
You can find LibGDX source code in LibGDXExamples!