我有一个名为InputHandler的类,它实现了InputProcessor,是我的游戏世界的InputProcessor。这很好用。但现在我正在尝试构建一个主菜单,我的clickListeners无法工作,而是调用来自我的InputHandler-class的touchDown()。我创建了一个所有屏幕的实例,以便能够轻松地在它们之间切换,但我不知道如何解决这个问题。我听说过InputMultiplexer,但我没有计划如何在我的代码中集成这样的东西来解决我的问题。我试图从我的touchDown()和其他方法返回false,但我的ClickListeners即使在那之后也什么也做不了。
下面是我的代码:
我创建所有屏幕的第一个“Main”类:
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));我的InputHandler:
公共类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实现了Screen {
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的实例。我如何解决根据当前显示的屏幕获取正确输入的问题?请尽可能详细地给出答案,因为我对所有这些东西都很陌生。对于那些乱七八糟的代码和我糟糕的英语,我深表歉意!
发布于 2014-12-31 13:23:45
在全局范围内,整个游戏只有一个InputProcessor。当您调用Gdx.input.setInputProcessor(new InputHandler(world));时,它将替换您在MainMenuScreen类中设置的InputProcessor。
一个简单的解决方案是每次在屏幕之间切换时更改游戏的输入处理器(在show()方法中)。
如果希望两个InputProcessors同时工作,则需要使用InputMultiplexer将它们组合在一起,并将多路复用器设置为游戏的InputProcessor。
https://stackoverflow.com/questions/27715655
复制相似问题