Skip to content

Stage Actor

Paulius Imbrasas edited this page Jan 16, 2014 · 1 revision

I highly recommend reading libgdx's entries on this Scene2d and Scene2d.ui in order fully understand the stage/actor logic, but this page will explain how we use it in Controller Concern.

Basic idea

As the aforementioned entries explain, every game screen contains a stage and actors. Every actor is added to the stage using a simple method such as stage.addActor(actor). Afterwards, every actor's draw method is called automatically by simply calling stage.draw(). Further so, by called stage.act(), every actor position and other assigned actions can be "processed".

Note: to reduce complexity of the code, in our code we call the act() method of actors (in our case - only the aircraft) manually

Stage

Every screen contains the most basic code (for this example we will use EndScreen.java):

root = new Stage();
Gdx.input.setInputProcessor(root);

These two lines initiate the stage and sets it as the Input Processor - this means every input (i.e. mouse and keyboard) will be handled by the stage.

Actors

An actor is anything that is interacted with, in our case - buttons, waypoints and aircraft.

Adding an actor, in our example, a label (i.e. text) is extremely simple:

Label text = new Label("Planes have collided!",
			Art.getSkin(), "textStyle");

ui.add(text).center();

ui is a table previous created, for more infomation read about TableLayout - this repository has everything required to find out how to use the built-in Table Layout library.

In our code we simply create it using Table ui = new Table(); and add it as an actor to the previously created stage: root.addActor(ui);

Making the Stage the Input Processor is an essential step and shouldn't be skipped, otherwise code such as

ui.addListener(new InputListener() {
	@Override
	public boolean keyDown(InputEvent event, int keycode) {
		if (keycode == Keys.ESCAPE)
			setScreen(new MenuScreen());

		return false;
	}
});

root.setKeyboardFocus(ui);

will not work. This adds a listener which handles keyboard input. The last line is required as only one actor in a stage can have keyboard focus (meaning only it "gets" the input from the keyboard).

Clone this wiki locally