libgdx使用手势监听器左右滑动检测

libgdx使用手势监听器左右滑动检测

本文介绍了Android libgdx使用手势监听器左右滑动检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 libgdx 在屏幕中央显示了一个图像.如果我向左滑动图像应该向左移动,如果我向右滑动图像应该向右移动.

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.

随后向左滑动应将图像向左移动.权利也应该如此.我使用了 GestureListener.

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.

那么我如何在 libgdx 中克服这个问题??

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

推荐答案

我使用了这个链接中的例子.https:///github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/GestureDetectorTest.java.

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

这篇关于Android libgdx使用手势监听器左右滑动检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 06:31