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 Stage
s.
The first,
stage
, contains a number of objects deriving fromActor
and added via the standardstage.addActor(Actor)
. This is for the actual game objects.The second,
hudStage
, consists of a singleTable
actor. This table includes a button as well as twoLabel
s. 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:
- An actor in
GameScreen.stage
is target of atouchDown
event. - Logic in actor fires and updates object.
- touchDown event in actor finishes by updating
infoLabel
inGameScreen.hudStage
or updates String value inGameScreen
(if easier, I'll go this route).
GameScreen
. It looks like the only window I have open is this answer on SO about multiple stages. Note that my cameras are different, which made me think two sibling stages would be required. \$\endgroup\$