libgdx multiple objects implementing InputProcessor(libgdx 多个对象实现 InputProcessor)
问题描述
所以在我的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:libgdx 多个对象实现 InputProcessor
基础教程推荐
- 如何强制对超级方法进行多态调用? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01