Justin Struk
Justin Struk

Reputation: 1

Check which actor is pressed? (libgdx/androidstudio)

I'm fairly new to android programming and libgdx/scene2d and all that good stuff. I have four arrow keys on the screen and am messing around with moving a "dude" around when they are pressed. I wanted to see if anyone knows of a way to have one InputListener, and inside that listener have a way to check which actor was pressed on the stage, and do something based on that, instead of having four different inputlisteners and touchup/down methods, one for each textbutton, I just want one. Here is what I have, and need to check inside this method which actor has been pressed. Thanks for reading and helping :D

InputListener inlis = new InputListener(){//Creating an InputListener to assign to each button instead of writing the same code four times :D
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            System.out.println("Press");
            return true;
        }

        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            System.out.println("Release");
        }
    };
    tbRight.addListener(inlis);
    tbLeft.addListener(inlis);
    tbDown.addListener(inlis);
    tbUp.addListener(inlis);

Upvotes: 0

Views: 417

Answers (1)

Rara
Rara

Reputation: 649

It's a good desire to write every piece of code just one time. And you can write it one time and then use it four (and more) times.

1) Create base class with InputListener methods:

public class Button extends Actor {
    int key;

    ... // All other Actor methods goes here

    public void addListeners() {
        mouseListener = new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                doAction();
                return true;
            }
        };
        addListener(mouseListener);

        keyboardListener = new InputListener() {
            @Override
            public boolean keyDown(InputEvent event, int keyCode) {
                if (event.getKeyCode() == key) {
                    doAction();
                    return true;
                } else {
                    return false;
                }
            }
        };
        getStage().addListener(keyboardListener);

    protected void doAction() {
        // This method is empty. We will override it later.
        // You can declare it abstract if you want.
    }
}

2) Then extend it with desired functionality of every button like this:

public class DownButton extends Button {
    public DownButton(float x, float y, int buttonSize) {
        super(x, y, buttonSize);
        key = Input.Keys.DOWN;
    }

    @Override
    protected void doAction() {
        System.out.println("Go down");
    }
}

3) And finally you can add this Actor to your stage:

    ...
    DownButton downButton = new DownButton(100, 100, buttonSize);
    stage.addActor(downButton);
    downButton.addListeners();
    ...

In this case you will need to write only 1 line of code to add listeners for every new button.

Upvotes: 1

Related Questions