Java 单击侦听器和InputHandler
我有一个名为Java 单击侦听器和InputHandler,java,android,input,libgdx,listeners,Java,Android,Input,Libgdx,Listeners,我有一个名为InputHandler的类,它实现了InputProcessor,是我游戏世界中的InputProcessor。那很好用。但现在我正试图构建一个主菜单,我的clickListeners不起作用,而是调用了来自我的InputHandler的触地()。我创建了一个我所有屏幕的实例,以便能够轻松地在它们之间切换,但我不知道如何解决这个问题。我听说过一个输入多路复用器,但我没有计划如何在我的代码中集成这样的东西来解决我的问题。我试图从touchtdown()和其他方法返回false,但我的
InputHandler
的类,它实现了InputProcessor
,是我游戏世界中的InputProcessor
。那很好用。但现在我正试图构建一个主菜单,我的clickListeners
不起作用,而是调用了来自我的InputHandler的触地()。我创建了一个我所有屏幕的实例,以便能够轻松地在它们之间切换,但我不知道如何解决这个问题。我听说过一个输入多路复用器
,但我没有计划如何在我的代码中集成这样的东西来解决我的问题。我试图从touchtdown()
和其他方法返回false,但我的ClickListeners
在这之后什么也不做
这是我的密码:
我的第一个“主”类,在这里我创建了所有屏幕:
public void create(){
mainMenuScreen = new MainMenuScreen(this);
gameScreen = new GameScreen(this);
setScreen(mainMenuScreen);
}
游戏类及其inputProcessor:
public GameScreen(final Stapler gam) {
this.game = gam;
world = new World(new Vector2(0, StaplerValues.WORLD_GRAVITY), true);
Gdx.input.setInputProcessor(new InputHandler(world));
我的InPurthandler:
公共类InputHandler实现InputProcessor{
World world;
public InputHandler(World world) {
this.world = world;
}
public boolean touchDown(int x, int y, int pointer, int button) {
// this is called even when i'm in my main menu and want to click a button
return false;
}
public boolean touchUp(int x, int y, int pointer, int button) {
// your touch up code here
return false; // return true to indicate the event was handled
}
public boolean touchDragged(int x, int y, int pointer) {
return false;
}
以及我的主菜单及其clickListeners:
公共类MainMenuScreen实现屏幕{
public MainMenuScreen(final Stapler gam) {
game = gam;
stage = new Stage();
table = new Table();
table.setFillParent(true);
stage.addActor(table);
Gdx.input.setInputProcessor(stage);
// Add widgets to the table here.
TextureRegion upRegion = new TextureRegion(new Texture(
Gdx.files.internal("boxLila.png")));
TextureRegion downRegion = new TextureRegion(new Texture(
Gdx.files.internal("boxGruen.png")));
BitmapFont buttonFont = new BitmapFont(
Gdx.files.internal("fonts/bodoque.fnt"), false);
buttonFont.setScale(2);
TextButtonStyle style = new TextButtonStyle();
style.up = new TextureRegionDrawable(upRegion);
style.down = new TextureRegionDrawable(downRegion);
style.font = buttonFont;
play = new TextButton("Play", style);
play.addListener(new ClickListener() {
public void clicked(InputEvent e, float x, float y) {
game.setScreen(game.gameScreen);
}
});
// add the button with a fixed width
table.add(play).width(500);
// then move down a row
table.row();
}
单击侦听器可以工作,但前提是我没有首先创建GameWorld的实例。我如何解决他们根据当前显示的屏幕获取正确输入的问题?请尝试给出尽可能详细的答案,因为我对所有这些内容都很陌生。对于混乱的代码和我糟糕的英语,我深表歉意。谢谢dvance!!!整个游戏全局只有一个InputProcessor。当您调用Gdx.input.setInputProcessor(新的InputHandler(世界));
时,它将替换您在MainMenuScreen类中设置的InputProcessor
一个简单的解决方案是每次在屏幕之间切换时(在show()
方法中)只需更改游戏的输入处理器
如果你想让两个输入处理器同时工作,你需要使用一个来组合它们,并将多路复用器设置为游戏的输入处理器