Click through an Actor in libGDX

心不动则不痛 提交于 2019-12-05 02:00:36

Use an InputMultiplexer. The InputMultiplexer class allows you to share user input among multiple input processors. Create your own class extending InputProcessor, and put that in InputMultiplexer with your Stage. That way you can respond to user input in a custom way, and still be able to use your stage.

    InputMultiplexer multiplexer = new InputMultiplexer();
    Array<InputProcessor> processors = new Array<InputProcessor>();
    MyInputProcessor myInputProcessor = new MyInputProcessor(); 
    processors.add(myInputProcessor);
    processors.add(stage);
    this.multiplex.setProcessors(processors);

    //...
    //and in your show method in your Screen class
    Gdx.input.setInputProcessor(this.multiplex);

Also, be sure to return null from Actor.hit. This should cause the actor to not respond to any user interaction.

This is how I solved this problem in my game.

In addition to the other methods note you can also call:

setTouchable(Touchable.disabled);

Which is documented as:

No touch input events will be received by the actor or any children.

Method is n the Actor class.

Grisgram

Yes, Pool is right.

Just set touchable to disabled.

It is questionable, whether it is a "good" default of the engine to make all actors touchable in a stage, because in most of my games the majority of actors is _not_ touchable, and there are only few elements the user can/shall interact with. Therefore I always create a base class of "nonTouchableActor" where I derive all my Actors from that shall not react on clicks/taps and this base class sets touchable(disabled) in the constructor. That way you no longer have to think about it.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!