问题描述
所以在我的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)