Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android libgdx swipe left and right detection using gesture listener

I have displayed a image at the center of the screen with libgdx. If i swipe left the image should move left and if i swipe right image should move 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 overcome this in libgdx??

    class MyGestureListener implements GestureListener {

        @Override
        public boolean fling(float arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub


              if(arg0>0)
               iX += 20;
              else
             // else if(arg0*100>iX)
                  iX-=20;
               System.out.println("Hello..............."+iX);
            return true;
        }

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

   batch.draw(splashTexture, iX, iY);
like image 361
Raghunandan Avatar asked Oct 04 '22 20:10

Raghunandan


1 Answers

I used the example in this link. https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/GestureDetectorTest.java.

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

          // Ignore the input, because we don't care about up/down swipes.
       }
 return true; 
}
like image 76
Raghunandan Avatar answered Oct 10 '22 02:10

Raghunandan