5
\$\begingroup\$

I'm trying to detect when a Group actor (my "slot") on my stage is clicked by add an InputerListener to the group. I've added the stage to my InputMultiplexer, but when I click on the actor, nothing happens.

Relevant code from the main Screen:

InputMultiplexer inputMultiplexer = new InputMultiplexer();
Stage stage = new Stage();
inputMultiplexer.addProcessor(stage);
Gdx.input.setInputProcessor(inputMultiplexer);

From the Group class:

Label amountLabel = new Label("", GameScreen.labelStyle);
Image itemImage = new Image();
Image slotImage = new Image(new TextureRegionDrawable(new TextureRegion(new Texture("Slot.png"))));
public SlotGroup() {
    this.addActor(amountLabel);
    this.addActor(itemImage);
    this.addActor(slotImage);

    this.addListener(new InputListener() {
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            System.out.println("Hello world");
            return true;
        }
    });
    this.setStage(Screen.stage);
}
\$\endgroup\$
0
\$\begingroup\$

The issue may be that the Group distributes the touch events rather than accepting them itself:

From https://libgdx.badlogicgames.com/nightlies/docs/api/

Touch events that hit more than one actor are distributed to topmost actors first.

Check whether adding the listener to a specific widget in the group will cause it to trigger instead. If so, then this is likely a design decision within the system.

\$\endgroup\$
-1
\$\begingroup\$

The multiplexer is of no use if you just add the stage to it. You have to add "this" to the multiplexer as well, which would be your main screen. The multiplexer can take several and the order you add them in decides which gets to handle the input first.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.