4

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");
    }
});
Daahrien
  • 10,190
  • 6
  • 39
  • 71
Skepte Cist
  • 41
  • 1
  • 3
  • 2
    What version of libgdx are you using? The latest doesn't seem to have `Stage.addListener()`. Maybe this is a bug that was fixed in a newer version than what you're using. – kichik Nov 02 '12 at 20:56
  • I'm using a nightly build from 10/20/2012, the libgdx.txt says its 0.9.3, I'll try 0.9.6 then. – Skepte Cist Nov 02 '12 at 21:19
  • Ah. Well, I'm less familiar with the bleeding edge. If I had to guess, I'd say implementing `touchDragged()` might help. – kichik Nov 02 '12 at 21:26
  • I just tried that too:), no luck though, it never fires. – Skepte Cist Nov 02 '12 at 21:33

2 Answers2

3

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.

sammy3597
  • 63
  • 5
1

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.

Daahrien
  • 10,190
  • 6
  • 39
  • 71
AlgoRhymes
  • 126
  • 2
  • 8