Libgdx touchUp event

I use actors with a scene as buttons. I can detect when touchDown / touchUp events happen just above the actor, but when the user clicks on the actor and then continues to push his finger away from the actor, the touchUp event never fires. Instead, I tried to use the exit event, but it never fires. In my program, touchUp / touchDown events determine the movement and color of buttons, which depend on whether the button is pressed or not. Thus, I remained with the button constantly pressed until he clicked up / down again.

Sample code that I use:

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"); } }); 
+4
source share
2 answers

If you change

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

to

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

It recognizes a TouchUp call. And he will still be able to handle TouchDown and Exit calls.

+3
source

I had the same problem. I fixed this by creating the boolean isDown variable as a field of my GameScreen class. Whenever touchDown appears on my background image , I make the variable isDown true, and when touchUp appears - isDown = false. Thus, touchUp will always be the case. Then, still in my GameScreen, in the rendering method, I check if isDown is true, if so, I check if the touch intersects with my player:

 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 is the pointIntersection method:

 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; } 

This is the only workaround I have found. However, this is not very pretty, but it works for me.

+1
source

Source: https://habr.com/ru/post/1443737/


All Articles