uday
uday

Reputation: 51

Libgdx Scene2d: Event Listener on Table

I wanted to know how you can detect a touch event on a table. Although I have tried using inputlistener. But it only gives me touch on the elements. But instead i want touch to be detected everywhere on the table. In short I want my table to be turned as Button.

Upvotes: 3

Views: 3740

Answers (1)

Madmenyo
Madmenyo

Reputation: 8584

You can add a listener to any actor on your stage. In your case I would suggest a ClickListener. You can add the ClickListener as a "anonymous class" like I did below. Override it's clicked method with your logic you can get rid of the call to .super as the base method does nothing. You have access to specific details in the InputEvent that is supplied such as which button is pressed.

    Table table = new Table();
    //Might just be this line to have the table interact.
    table.setTouchable(Touchable.enabled); 
    t.addListener(new ClickListener(){
        @Override
        public void clicked(InputEvent event, float x, float y) {
            System.out.println("I got clicked!");
        }
    });

Of course your table needs to be expanded in order to click it. If you are not able to click it you should run table.debug() and make sure you see the table bounds on your screen and click within these.

Upvotes: 10

Related Questions