LIBGDX Android - 按钮监听器无法工作

3

我一直在尝试向我的游戏中添加一个按钮,即使我查找了所有的解决方案,似乎什么都不起作用。

按钮可以正常渲染,但是当我点击它时却没有任何反应。

我一直在使用一个 InputMultiplexer 在舞台上设置输入处理器,并将按钮添加为演员。

任何帮助将不胜感激。

this.stage = new Stage();

...

this.buttonReplay = new TextButton("Replay", buttonStyle);
    this.buttonReplay.setX(width / 2 - this.buttonReplay.getPrefWidth() / 2);
    this.buttonReplay.setY(height / 2 - this.buttonReplay.getPrefHeight() / 2);

this.stage.addActor(buttonReplay);

(不同的类)

        multiplexer.addProcessor(menuDeath.getStage()); // Adds death menu to input processor

        Gdx.app.log("adddad", "added");

        menuDeath.getButtonReplay().addListener(new InputListener()
        {
            @Override
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button)
            {
                Gdx.app.log("dsad", "daDSAda");
                return true;
            }
            @Override
            public void touchUp (InputEvent event, float x, float y, int pointer, int button)
            {
                Gdx.app.log("dsad", "daDSAda");
            }
        });

当运行时,“added”会在控制台中记录,这应该意味着stage有一个输入处理器,然而,按钮似乎根本不起作用,我甚至尝试使用不同的监听器,如ClickListenerChangeListener
1个回答

1
多路复用器本可以将输入事件先发送给另一个侦听器,该侦听器通过返回 true 来消费输入事件。
您可以首先尝试消除多路复用器,以确保代码可以正常工作,方法是替换。
multiplexer.addProcessor(menuDeath.getStage());

使用

Gdx.input.setInputProcessor(menuDeath.getStage());

那样你的menuDeath阶段就是唯一的监听器,因此应该正确处理touchDown事件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接