1
\$\begingroup\$

I'm making a game where I have buttons on a stage, but the background also acts as a "button". But when I press on one of the existing actors on the stage, it also registers a click in the background in addition to the click on the button. How can I fix this?

Inside constructor

button = new ImageButton(getDrawable(new Texture("PlayUp.jpg"))); //only using one image atm
button.setSize(150, 134);
button.setPosition(camera.position.x, camera.position.y);
stage = new Stage(viewport);
        Gdx.input.setInputProcessor(stage);
        stage.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                System.out.println("Background touched");

            }
        });
        button.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                System.out.println("Button touched");
            }
        });
        stage.addActor(button);

part of render() method

@Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    tapCatch.batch.setProjectionMatrix(camera.combined);
    tapCatch.batch.begin();

    stage.act(delta);
    stage.draw();

    tapCatch.batch.end();


}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

It the click-handler for the background, make sure you filter out the InputEvents that are already handled:

Gdx.input.setInputProcessor(stage);
stage.addListener(new ClickListener(){
    @Override
    public void clicked(InputEvent event, float x, float y) {
        if (!event.isHandled()) {    
            System.out.println("Background touched");
        }
    }
});

This will allow you to only care about events that are not already handled by a previous click-handler, like your Button for example.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .