У меня есть класс InputHandler
, который реализует InputProcessor
и является InputProcessor
для моего игрового мира. Это прекрасно работает. Но сейчас пытаюсь построить главное меню и мои clickListeners
не работают, а вместо них вызывается touchDown()
из моего InputHandler
-класса. Я создал экземпляр всех своих экранов, чтобы можно было легко переключаться между ними, но я понятия не имею, как это исправить. Я слышал о InputMultiplexer
, но у меня нет плана, как интегрировать такую вещь в мой код для решения моих проблем. Я пытался вернуть false из моего touchDown()
и других методов, но мой ClickListeners
вообще ничего не делает даже после этого.
Вот мой код:
1-й мой «Основной» класс, где я создаю все экраны:
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 реализует 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. Как я могу решить, что они захватывают правильный ввод в зависимости от того, какой экран отображается в данный момент? Пожалуйста, попробуйте дать ответ как можно более подробно, потому что я совершенно новичок во всем этом. И извините за этот беспорядок в коде и мой плохой английский, заранее спасибо!!!