Kae*_*zon 1 java input lwjgl libgdx scene2d
我使用lwjgl渲染游戏环境,同时使用scene2d.UI构建一个包含输入的表.
目前,如果我点击UI它确实会做出反应,但游戏环境也是如此.显然是因为我没有处理它.这里的问题是-where-我应该处理输入,以便鼠标点击UI也不会激活环境的输入处理.
我的想法是将一个监听器附加到UI表并将输入处理移动到监听器.我只是不知道如何设置它.我应该在桌面上设置一个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; }
}
Run Code Online (Sandbox Code Playgroud)
这就是我目前正在构建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();
//------------------------------------------//
Run Code Online (Sandbox Code Playgroud)
不要将轮询机制用于input(Gdx.input.isXXX(...)).它不是很稳定,因为你可能会错过一些事件,它也依赖于帧速率.
InputProcessor为您的游戏输入实现一个.然后使用InputMultiplexer让两个处理器(Stage和你的GameplayInputProcessor)都有机会处理事件.
这应该如下所示:
InputMultiplexer multiplexer = new InputMultiplexer();
multiplexer.addProcessor(stage);
multiplexer.addProcessor(gameplayInputProcessor);
Gdx.input.setInputProcessor(mutliplexer);
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您的舞台将首先获得活动.如果您的UI已经处理过,您不希望第二个处理器接收输入事件,那么您需要return true处理阶段的处理方法.