I am trying to implement fling guestures in a game. However, it seems that the fling gestures are not detected (I am using Android emulator).
I'm not sure what is wrong as I have debugged it and the gestures are not being detected.
Here is my code, I apologize as it is kind of messy.
public class PlayActivity extends Activity {
private static final String TAG = "PlayActivity";
Maze maze;
GameView gameView;
ViewConfiguration vc;
int swipeMinDistance;
int swipeThresholdVelocity;
int swipeMaxOffPath;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
/**
* Sets up UI
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameView = new GameView(this);
gameView.setKeepScreenOn(true);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(gameView);
this.maze = MazeContainer.getMaze();
ViewConfiguration vc = ViewConfiguration.get(getApplicationContext());
swipeMinDistance = vc.getScaledPagingTouchSlop();
swipeThresholdVelocity = vc.getScaledMinimumFlingVelocity();
swipeMaxOffPath = vc.getScaledTouchSlop();
gestureDetector = new GestureDetector(this, new MyGestureDetector());
gestureListener = new View.OnTouchListener() { //I don't know if I set this part up correctly
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
Toast.makeText(this, "Play Activity", Toast.LENGTH_LONG).show();
}
/**
* Makes sure that once back button is pressed, Title activity will start
*/
@Override
public void onBackPressed() {
Log.v(TAG, "Back button pressed. Going back to AMazeActivity.");
Intent intent = new Intent(getApplicationContext(), AMazeActivity.class);
startActivity(intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
finish();
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { //This method never gets executed
try {
if (Math.abs(e1.getY() - e2.getY()) > swipeMaxOffPath)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > swipeMinDistance && Math.abs(velocityX) > swipeThresholdVelocity) {
// Toast.makeText(SelectFilterActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
maze.keyDown(null, 'h');
} else if (e2.getX() - e1.getX() > swipeMinDistance && Math.abs(velocityX) > swipeThresholdVelocity) {
// Toast.makeText(SelectFilterActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
maze.keyDown(null, 'j');
}
} catch (Exception e) {
// nothing
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
}
{
A suspicious area might be instantiating gesturesListener
. But since the debugger is not telling me anything since nothing is going wrong, I don't know for sure. All I know is that dragging and accelerating the mouse over the emulator does not execute the appropriate methods inside MyGestureDetector
.
Any tips is appreciated!
The one piece that you are missing is that you are not sending the activity's touch events to your listener. Assuming that you want to detect gestures on your GameView object, the one line you need, after constructing the listener, is:
gameView.setOnTouchListener(gestureListener);