libgdx multiple objects implementing InputProcessor

前端 未结 2 479
悲&欢浪女
悲&欢浪女 2021-01-04 03:58

So on my Screen I have two objects of the same class that implement InputProcessor with the following keyDown() method:



        
2条回答
  •  攒了一身酷
    2021-01-04 04:50

    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);
    

    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.

提交回复
热议问题