Search code examples
libgdxlistener

Confused about Inputlistener LIBGDX


I have

  • Main Test Class creating a Stage, Adding an Actor to the stage and setting the Inputprocessor to the stage

  • extended Group Class with several Actors added. In the Constructor of the Group I have added an InputListener.

The InputListener is not fired . Can someone tell why not and how to do ?

public class Test extends ApplicationAdapter implements ApplicationListener {

    public void create() {
        stage = new Stage(new ScreenViewport());
        specialScene = new SpecialScene();
        stage.addActor(specialScene);    
    Gdx.input.setInputProcessor(stage);

    }


}


public class SpecialScene extends com.badlogic.gdx.scenes.scene2d.Group {
    public SpecialScene {
        <add some actors ...>
        addListener(specialListener);       
    }

    private static InputListener specialListener = new InputListener() {
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button)        {
            return true; //or false
        }

        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            super.touchUp(event, x, y, pointer, button);
        }

        @Override
        public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
            super.enter(event, x, y, pointer, fromActor);
        }
    };

}

* UPDATE *

I found the problem. The Listener did not find any region of my Actors. I have to set explicitely the Region with setBounds().

My Problem is solved, but I am still confused. Why do I have to set the bounds myself. I will forget this with every Actor in the future I am sure, because it is unlogical to me. Is this the way I have to, or do I understand the concept wrong ?


Solution

  • Group is a skeleton class that can be used to develop your own functionality, so it does not presume anything, even the way its child actors contribute to its bounds. (For example, you might have some actors that you don't want to contribute because they are a visual flourish, like particles.) You can extend Group to create your own base class to fit your needs.

    So why doesn't LibGDX already include a class like that? In LibGDX, Stage is used primarily for the UI system. Although it was designed to be extensible to all kinds of purposes, it only includes a framework for you to do that, unless you are using the fully baked UI implementation that is based on it. That UI implementation does include a subclass of Group called WidgetGroup, which does what you'd expect with the bounds.

    IIRC, the author of Stage wrote a blog post a few years ago on libgdx.com discussing how he tried using Stage for the gameplay of a simple game jam game, and basically concluded that it caused his game to be more convoluted, or at least more time-consuming to code.

    I have personally used it for a turn-based game jam game, and it was good for that. I used the Actions system to have nice animated transitions of the game pieces. But I think it would make a real-time game more convoluted than creating your own organization structure that is tailored to your particular game. If you are creating a more complicated game, you might check out the Ashley plugin for LibGDX.

    In either case, you definitely should use it for GUI stuff because that is all fully implemented and a huge time-saver.