# LibGDX Swipe Detection Left and Right

I have displayed an image at the center of the screen with libgdx. If I swipe left, the image should move to the left; and, if I swipe right, image should move to the right.

Subsequent swipes to the left should move the image left. The same should happen for right. I used GestureListener.

It works to some extent in the sense if I swipe left first image moves left. But after that if I try to swipe right the image still moves left.

So how do I implement the expected behavior?

class MyGestureListener implements GestureListener {
@Override
public boolean fling(float velocityX, float velocityY, int button) {
if (velocityX>0)
iX += 20;
else
iX -= 20;
System.out.println("iX: "+iX);
return true;
}
}

// ...
Gdx.input.setInputProcessor(new GestureDetector(0.0f, 0.0f, 0.0f, 5f, new MyGestureListener()));

// ...
batch.draw(splashTexture, iX, iY);

• That bug could be caused by anything. What does your code currently do? Why do you think it's not working? – Anko Mar 2 '13 at 23:02
• I will post my code. – Raghunandan Mar 3 '13 at 4:01
• i can't understand why my question is downvoted. Is there something missing or something to be corrected. – Raghunandan Mar 4 '13 at 18:54
• I also wished downvoters always left comments. My guess: Maybe because the question is localised: Would seeing how to fix your particular bug help anyone else? Maybe also because it's confusing: It's hard to tell what the code is doing. It is generally helpful to condense the question and clean up the code. Users with enough reputation are able to edit posts, but it's hard to do without completely understanding the original. – Anko Mar 4 '13 at 22:08

There are a few potential problems here. Lets take a look at your code first:

if(arg0>0)
iX += 20;
else
iX-=20;


arg0 is the velocityX of the last known horizontal velocity of the finger in pixels per second. Here's the method signature for fling():

fling

boolean fling(float velocityX, float velocityY, int button)

Called when the user dragged a finger over the screen and lifted it. Reports the last known velocity of the finger in pixels per second.

 Parameters:
velocityX - velocity on x in seconds
velocityY - velocity on y in seconds


So, the method is not taking in ALL the data of the swipe, it's merely taking the final position info. What happens if you ended your swipe without any horizontal velocity? According to your code, the answer would be, move left because the X velocity in the final position would be zero.

if(arg0>0)
iX += 20;
else   // This includes X == 0 because you only checked greater than!
iX-=20;


What happens if you swipe upwards? According to your code, the final position would be move left, because arg1 has the upwards movement and is being ignored. What happens if you swipe downwards? According to your code, the final position would be move left. What happens if you swipe a little to the right and a little to the left? Move left.

I think what you want to do instead is something along the following lines:

   @Override
public boolean fling(float velocityX, float velocityY, int button) {
if(Math.abs(velocityX)>Math.abs(velocityY)){
if(velocityX>0){
iX+=20;
}else if (velocityX<0){
iX-=20;
} else {
// Do nothing.
}
}else{

// Ignore the input, because we don't care about up/down swipes.
}
return true;
}


• Debugging: For debugging, I recommend that you log not only the resulting iX, but also the incoming arg0 (velocityX) values. I strongly suspect that a few minutes of looking at the input data will resolve the issue. In particular, you're going to want to know if this method is receiving zero values (which with your code will move the image left), if it's receiving non-negative values when it should be receiving negative values, and so forth.

• Set the MaxFlingDelay: A word about fling(): fling() is not always called. You need to set the maxFlingDelay in the GestureDetector constructor for the amount you want to trigger a fling(), otherwise it uses a default value, which you may not be happy with.

• Consider Pan and Other Alternatives:Consider alternatives to fling() including pan(). As has been noted by Benjamin, you may want pan() instead (or something else). That's an excellent suggestion and possible solution.

• Run a Gesture Detector Test: To help decide on whether to use fling() and to see if fling() is giving the right values, play around with this gesture detector test.

• Consider the Return Boolean: When you return true in this method, you're signalling that the input has been handled and shouldn't be processed further. That's not descriptive of the code sample I gave you (where up/down flings are ignored, for example). You may want to modify it to put the proper return trues/return falses in the appropriate places, so you don't run into unexpected behavior later if you implement other touch handling for nearby entities.

• Variable Names: This is just a minor pet-peeve. It makes things much easier to read if you use descriptive variable names (like velocityX, velocityY, above) instead of Arg0, Arg1, iX, and so on. You asked above why you received a downvote. Don't know why somebody gave it, but sometimes code with bad naming conventions rubs people the wrong way.

• while it is not specified, I think the asker was not interested in vertical velocity (so left diagonal swipes would work). It definitely is good to check for dominate vertical movement, but I think (s)he was going for any change in x position in this example. – Benjamin Danger Johnson Mar 4 '13 at 19:34
• @BenjaminDangerJohnson I agree with you. Didn't my code sample (e.g., "// Ignore the input, because we don't care about up/down swipes.") suggest I agreed? – Cameron Fredman Mar 4 '13 at 19:35
• I do see that, but it looks like you are saying, do nothing if flight is mostly vertical (so anything more than 45 degrees away from the X-axis would be ignored). – Benjamin Danger Johnson Mar 4 '13 at 19:40
• Well, to some extent i'm guessing at the problem. If the method receives a negative value for arg0 it should be setting a negative value for iX in his code. My guess was that slightly different inputs are coming as a result of slightly different swipes, including up and downs, as well as zero X's, as well as possible values in maxFlingDelay. – Cameron Fredman Mar 4 '13 at 19:50
• @Raghunandan You gonna let that bounty go to waste? :) (Hint hint) – Cameron Fredman Mar 9 '13 at 16:51

Looking at an example I found online, it seems you might be extending the wrong class. It looks like the preferred class to extend is GestureDetector rather than GestureListener (although I am not sure the exact difference) the example I am looking at has a pretty good implementation for fling handling (although it is pretty similar to the answer provided by Cameron Fredman). My other suggestion was to consider switching from fling, to pan (as this is usually used for panning around an image which it seems like you are trying to do.) If possible can you give us some debug information for flinging around? just what values are returned from a sequence of calling fling 2 times in each direction should work.

• "From what I can tell, fling returns the absolute value of the x and y velocities." I don't think that's correct. – Cameron Fredman Mar 4 '13 at 19:48
• well, I was kind of guessing that because you said you were only able to move left. I am probably wrong with that assumption. Although you may want to consider using Pan instead. – Benjamin Danger Johnson Mar 4 '13 at 19:51
• @CameronFredman I updated my answer to show an example of fling I found online. I'm actually pretty sure you are right in that it is not the absolute values, but since there is no debug data I can't be sure. Also he said it was always flinging left which to me suggested absolute value velocities. While my old answer could be right, I am a bit skeptical myself. – Benjamin Danger Johnson Mar 4 '13 at 20:01
• all fling towards right velocity X values are 0.12..., 0.13... and 0.134..... All fling towards left velocity X values are 0.03..,0.04..,0.002....The fling works sometimes and sometimes it does not work. I tried printing values before the if condition. – Raghunandan Mar 5 '13 at 1:31