LIBGDX Android - 按钮侦听器不工作
LIBGDX Android - button listener not working
我一直在尝试在我的游戏中添加一个按钮,但即使在我查找了所有解决方案之后,似乎也没有任何效果。
按钮呈现良好,但当我触摸它时它没有任何反应。
我一直在使用 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);
(不同 class)
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
有一个输入处理器,但是,按钮似乎根本不起作用,我什至尝试过使用不同的侦听器,例如 ClickListener
和 ChangeListener
多路复用器可以先将输入事件发送给另一个侦听器,该侦听器通过返回 true 来消耗输入事件。
您可以先尝试摆脱多路复用器,只是为了确保您的代码可以正常工作,方法是更换
multiplexer.addProcessor(menuDeath.getStage());
和
Gdx.input.setInputProcessor(menuDeath.getStage());
这样你的 menuDeath 阶段是唯一的监听器,因此应该正确处理 touchDown 事件。
我一直在尝试在我的游戏中添加一个按钮,但即使在我查找了所有解决方案之后,似乎也没有任何效果。 按钮呈现良好,但当我触摸它时它没有任何反应。
我一直在使用 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);
(不同 class)
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
有一个输入处理器,但是,按钮似乎根本不起作用,我什至尝试过使用不同的侦听器,例如 ClickListener
和 ChangeListener
多路复用器可以先将输入事件发送给另一个侦听器,该侦听器通过返回 true 来消耗输入事件。
您可以先尝试摆脱多路复用器,只是为了确保您的代码可以正常工作,方法是更换
multiplexer.addProcessor(menuDeath.getStage());
和
Gdx.input.setInputProcessor(menuDeath.getStage());
这样你的 menuDeath 阶段是唯一的监听器,因此应该正确处理 touchDown 事件。