libgdx实现InputProcessor的多个对象 [英] libgdx multiple objects implementing InputProcessor

查看:86
本文介绍了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.

推荐答案

您需要使用

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的多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆