libgdx 多个对象实现 InputProcessor

libgdx multiple objects implementing InputProcessor(libgdx 多个对象实现 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)