简体   繁体   English

libgdx实现InputProcessor的多个对象

[英]libgdx multiple objects implementing InputProcessor

So on my Screen I have two objects of the same class that implement InputProcessor with the following keyDown() method: 所以在我的Screen我有两个同一类的对象, InputProcessor使用以下keyDown()方法实现InputProcessor

@Override
public boolean keyDown(int keycode) {
    if (keycode==fireKey) {
        System.out.println("Reporting keydown "+keyCode);
    }
    return false;
}

The problem is when I instantiate these two objects, only the last one instantiated receives any keyDown events. 问题是当我实例化这两个对象时,只有最后一个实例接收到任何keyDown事件。 I need both objects (or however many there are) to receive keyDown events. 我需要两个对象(或者有许多对象)来接收keyDown事件。

You need to use an InputMultiplexer to forward the events to both InputProcessors . 您需要使用InputMultiplexer将事件转发到两个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);

The multiplexer works like some kind of switch/hub. 多路复用器的工作方式与某种开关/集线器类似。 It receives the events from LibGDX and then deletegates them to both processors. 它从LibGDX接收事件,然后将它们删除到两个处理器。 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. 如果第一个处理器在其实现中返回true ,则意味着事件已完全处理,并且不再将其转发到第二个处理器。 So in case you always want both processors to receive the events, you need to return false . 因此,如果您始终希望两个处理器都接收事件,则需要返回false

Here is a code sample on LibGDX.info showing how to implement multiplexing with LibGDX: 以下是LibGDX.info上的代码示例,展示了如何使用LibGDX实现多路复用:

https://libgdx.info/multiplexing/ https://libgdx.info/multiplexing/

I hope it helps 我希望它有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM