我使用 lwjgl 渲染游戏环境,同时使用 scene2d.UI 构建一个包含我的输入的表格。
目前,如果我点击 UI,它会使用react,但游戏环境也会使用react。显然是因为我没有处理过。 这里的问题是我应该在哪里处理输入,以便在 UI 上单击鼠标不会同时激活环境的输入处理。
我的想法是给UI表附加一个监听器,把输入处理移到监听器上。我只是不知道如何设置它。 我应该在 Table 的子项上设置 ClickListener,还是我应该在这里做其他事情?
这是在渲染循环中运行的方法:
private void processInput() {
//Handle user input here
//[...]
//Mouse input
if(Gdx.input.isButtonPressed(Input.Buttons.LEFT) && lastClick == null) {
//lastClick = new Vector2(Gdx.input.getX() + CameraOrigen().x, (CameraOrigen().y+camera.viewportHeight)-Gdx.input.getY());
lastClick = ScreenToWorld(new Vector2(Gdx.input.getX(),Gdx.input.getY()));
gridIntersect = WorldToTileCoord(lastClick);
}
else if (!Gdx.input.isButtonPressed(Input.Buttons.LEFT) && lastClick != null) { lastClick = null; }
}
这就是我目前构建 UI 的方式:
//------------- UI Construction ------------//
table.align(Align.top);
Table debugTable = new Table();
debugTable.add(debugMouseTable);
debugTable.add(debugTileSelectTable);
table.add(debugTable).expandX().height((float)(game.VIRTUAL_HEIGHT * 0.85));
table.add(spTest).width((float)(game.VIRTUAL_WIDTH * 0.2)).height((float)(game.VIRTUAL_HEIGHT * 0.85)).bottom();
table.row();
table.add(sbTileTypes).align(Align.left).expandY();
table.add("corner");
spDebug.pad(10).defaults().expandX().space(4);
for (AtlasRegion aReg : hmTiles.get(TileType.FLOOR)) {
spDebug.add(new Button_TilePreview(aReg, skin, sbTileTypes.GetSelectionValue(), this, brush));
spDebug.row();
}
spDebug.invalidateHierarchy();
//------------------------------------------//
最佳答案
不要对输入使用轮询机制(Gdx.input.isXXX(...)
)。它不是很稳定,因为您可能会错过一些事件,而且它还依赖于帧率。
同时为您的游戏输入实现一个 InputProcessor
。然后使用 InputMultiplexer
让两个处理器(Stage
和您的 GameplayInputProcessor
)都有机会处理事件。
这应该如下所示:
InputMultiplexer multiplexer = new InputMultiplexer();
multiplexer.addProcessor(stage);
multiplexer.addProcessor(gameplayInputProcessor);
Gdx.input.setInputProcessor(mutliplexer);
这样您的舞台将首先获得事件。如果您不希望第二个处理器在您的 UI 已经处理了输入事件的情况下接收输入事件,那么您需要在该阶段的处理方法中返回 true
。
关于java - LIBGDX:如何在混合 2D/3D 设置中拦截 UI 上的点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22929007/