5

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); 
4
  • can you post your code snippet... Commented Mar 2, 2013 at 17:26
  • You can get you answer from [this old post][1] [1]: stackoverflow.com/questions/937313/… Commented Mar 2, 2013 at 17:31
  • Does that work with libgdx?? Commented Mar 2, 2013 at 17:33
  • dont know but you can try once.. Commented Mar 3, 2013 at 4:02

2 Answers 2

4

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; } 
Sign up to request clarification or add additional context in comments.

Comments

-1
 public class Test extends Activity{ private GestureDetector gesturedetector = null; View layout; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); layout = (LinearLayout)findViewById(R.id.container); gesturedetector = new GestureDetector(new MyGestureListener()); layout.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { gesturedetector.onTouchEvent(event); return true; } }); } public boolean dispatchTouchEvent(MotionEvent ev){ super.dispatchTouchEvent(ev); return gesturedetector.onTouchEvent(ev); } class MyGestureListener extends GestureDetector.SimpleOnGestureListener{ private static final int SWIPE_MIN_DISTANCE = 150; private static final int SWIPE_MAX_OFF_PATH = 100; private static final int SWIPE_THRESHOLD_VELOCITY = 100; @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { float dX = e2.getX()-e1.getX(); float dY = e1.getY()-e2.getY(); if (Math.abs(dY)<SWIPE_MAX_OFF_PATH && Math.abs(velocityX)>=SWIPE_THRESHOLD_VELOCITY && Math.abs(dX)>=SWIPE_MIN_DISTANCE ) { if (dX>0) { Toast.makeText(getApplicationContext(), “Right Swipe”, Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), “Left Swipe”, Toast.LENGTH_SHORT).show(); } return true; } else if (Math.abs(dX)<SWIPE_MAX_OFF_PATH && Math.abs(velocityY)>=SWIPE_THRESHOLD_VELOCITY && Math.abs(dY)>=SWIPE_MIN_DISTANCE ) { if (dY>0) { Toast.makeText(getApplicationContext(), “Up Swipe”, Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), “Down Swipe”, Toast.LENGTH_SHORT).show(); } return true; } return false; } } } 

2 Comments

This answer is for vanilla Android, not Libgdx. It also doesn't do anything that the original question doesn't already do.
no explanation just code. Could you explain what you did how the code works and solves the asked problem

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.