问题描述
所以在我的 Screen
上,我有两个相同类的对象,它们使用以下 keyDown()
方法实现 InputProcessor
:
So on my Screen
I have two objects of the same class that implement InputProcessor
with the following keyDown()
method:
@Override
public boolean keyDown(int keycode) {
if (keycode==fireKey) {
System.out.println("Reporting keydown "+keyCode);
}
return false;
}
问题是当我实例化这两个对象时,只有最后一个实例化的对象接收到任何 keyDown
事件.我需要两个对象(或者有多少)来接收 keyDown
事件.
The problem is when I instantiate these two objects, only the last one instantiated receives any keyDown
events. I need both objects (or however many there are) to receive keyDown
events.
推荐答案
你需要使用一个 InputMultiplexer
将事件转发到两个 InputProcessors
.它看起来像这样:
You need to use an InputMultiplexer
to forward the events to both InputProcessors
. It will look like this:
InputProcessor inputProcessorOne = new CustomInputProcessorOne();
InputProcessor inputProcessorTwo = new CustomInputProcessorTwo();
InputMultiplexer inputMultiplexer = new InputMultiplexer();
inputMultiplexer.addProcessor(inputProcessorOne);
inputMultiplexer.addProcessor(inputProcessorTwo);
Gdx.input.setInputProcessor(inputMultiplexer);
多路复用器的工作方式类似于某种交换机/集线器.它从 LibGDX 接收事件,然后将它们删除到两个处理器.如果第一个处理器在他的实现中返回 true
,这意味着该事件已完全处理并且不会再转发给第二个处理器.因此,如果您总是希望两个处理器都接收事件,则需要返回 false
.
The multiplexer works like some kind of switch/hub. It receives the events from LibGDX and then deletegates them to both processors. In case the first processor returns true
in his implementation, it means that the event was completely handled and it won't be forwarded to the second processor anymore. So in case you always want both processors to receive the events, you need to return false
.
这篇关于libgdx 多个对象实现 InputProcessor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!