Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

How can I update a Stage-based HUD from another Stage's Actor in libGDX?

$
0
0

I feel like this is a simple problem, but I’m having issues finding the correct search terms.

I have a Screen that includes two Stages.

  1. The first, stage, contains a number of objects deriving from Actor and added via the standard stage.addActor(Actor). This is for the actual game objects.

  2. The second, hudStage, consists of a single Table actor. This table includes a button as well as two Labels. This is for the static user interface/HUD.

I’m handling touch events on my custom actors in the game stage like the following:

public class GameObject extends Actor {
    private final static String TAG = GameObject.class.getName();
    // ...

    public GameObject() {
        // ...

        addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                Gdx.app.log(TAG, "touchDown on GameObject actor: <" + x + "," + y + ">");
                return super.touchDown(event, x, y, pointer, button);
            }
        });
    }

This works perfectly and logs what I need. However, I would now like to update the HUD with information about the object the user touched.

My screen has defined the Label I want to update (added via hudStage), but I’m not sure how to go from an actor in stage to the screen, or to the hudStage label.

public class GameScreen implements Screen {
    Stage stage;
    Stage hudStage;
    // This is the label I want to update.
    Label infoLabel;
    // ...
}

I know I can call setText(String) on the label, but I’m not sure the best way to bubble the click up. Plenty of examples have simplified versions where objects are defined in the main class (in my case, GameScreen) and are able to refer to them that way, but I’ve already broken these apart.

I started to look at creating a custom event that my actor could trigger, and that the stage would handle, but documentation is sparse and I’m not sure this is required.

I’ve also thought about scrapping the idea of having a label in the hudStage and instead having my actor draw text where the HUD would have. However, from what I continue to see, I think Scene2D is the right direction to go for the UI.

Thanks!

EDIT: What I’m looking for:

  1. An actor in GameScreen.stage is target of a touchDown event.
  2. Logic in actor fires and updates object.
  3. touchDown event in actor finishes by updating infoLabel in GameScreen.hudStage or updates String value in GameScreen (if easier, I’ll go this route).

Viewing all articles
Browse latest Browse all 434

Trending Articles