Kharak
Kharak

Reputation: 72

How to use touchup and touchDown with buttons

I have 3 buttons and i am trying to do some stuff on touchup and touchdown,i read many stuff in stackoverflow but its confusing i tried

@Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        if(button1.getX()==screenX&&button1.getY()==screenY) {
            Gdx.app.log("button", "touchdown");
        }
        return true;
    }

i also tried hit condition too and don't know right way to use it.plz help me with any suggestion

Upvotes: 0

Views: 2180

Answers (2)

Gokul Sreenivasan
Gokul Sreenivasan

Reputation: 479

if your using input processor , you have to set the input processor to the class . this can be done by the follwing code.

       //in your create method
       Gdx.input.setInputProcessor(this);

Upvotes: 0

Marius
Marius

Reputation: 412

I don't understand your question. You can add a listener to your button which can catch the touchdown / touchup / checked state of a button:

Button playButton = new Button();    
playButton.addListener(new InputListener() {
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            return true;
        }
});

Have a look at the InputListener API, to find an explanation what these states are. For example the touchdown method:

"Called when a mouse button or a finger touch goes down on the actor. If true is returned, this listener will receive all touchDragged and touchUp events, even those not over this actor, until touchUp is received. Also when true is returned, the event is handled}."

You can also set different Button images for these states: Link


Create one InputListener and attach it to each of the three buttons. When you receive for example a touchDown, you can use the event parameter to get the listenerActor and check which button was used:

public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
    if (event.getListenerActor().equals(playButton)) {

    }
}

Haven't tested it, but it should work:

public class Example {

    private Button button1;
    private Button button2;
    private Button button3;

    public Example() {
        MyListener listener = new MyListener();
        // create the buttons...
        button1.addListener(listener);
        button2.addListener(listener);
        button3.addListener(listener);
    }

    private class MyListener extends InputListener {

        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            if (event.getListenerActor().equals(button1)) {
                Gdx.app.log("Example", "button 1 touchdown");
            } else if (event.getListenerActor().equals(button2)) {
                Gdx.app.log("Example", "button 2 touchdown");
            } else if (event.getListenerActor().equals(button3)) {
                Gdx.app.log("Example", "button 3 touchdown");
            }
            return super.touchDown(event, x, y, pointer, button);
        }
    }
}

Upvotes: 1

Related Questions