Skepte Cist
Skepte Cist

Reputation: 41

libgdx touchUp event

I'm using an actors with a stage as buttons. I can detect when touchDown/touchUp events occur just fine over the actor, but when the user taps on the actor and then proceeds to drag their finger off the actor, the touchUp event never fires. I tried to use the exit event instead, but it never fires. In my program the touchUp/touchDown events determine movement and also button color which depend on whether the button is being pressed or not. So I' left with a permanently "depressed" button until it's clicked down/up again.

Example of the code I'm using:

stage.addListener(new InputListener() {

    public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
        Actor actor = stage.hit(x, y, true);
        if (actor != null){
            System.out.println("touchDown: " + actor.getName().toString()); 
        }
        return true;
    }

    public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
        Actor actor = stage.hit(x, y, true);
        if (actor != null){
                System.out.println("touchUp: " + actor.getName().toString());           
                }
        }

    public void exit(InputEvent event, float x, float y, int pointer, Actor toActor){
        System.out.println("exit");
    }
});

Upvotes: 4

Views: 5806

Answers (2)

sammy3597
sammy3597

Reputation: 63

If you change

stage.addListener(new InputListener() {});

to

stage.addListener(new ClickListener() {});

it will recognize the TouchUp call. And it still will be able to handle the TouchDown, and Exit calls.

Upvotes: 3

AlgoRhymes
AlgoRhymes

Reputation: 124

I had the same problem. I fixed it by creating boolean isDown variable as a field of my GameScreen class. Whenever touchDown occures on my background Image I make isDown variable true and when touchUp occures - isDown = false. That way touchUp will always occur. Then still in my GameScreen in render method i check if isDown is true, if it is, I check if the touch intersects with my actor:

if (isDown) {
   if (pointIntersection(myActor, Gdx.input.getX(), Gdx.input.getY())) {
                // do something
   }
} else {
  // reverse the effect of what you did when isDown was true
}

where pointIntersection method is:

public static boolean pointIntersection(Image img, float x, float y) {
    y = Gdx.graphics.getHeight() - y;
    if (img.x <= x && img.y <= y && img.x + img.width >= x && img.y + img.height >= y)
        return true;

    return false;
}

It's the only workaround I found to that. Still, it's not very pretty but works for me.

Upvotes: 1

Related Questions