Question

So, I have the following body for my onFling() method:

    public boolean onFling(MotionEvent e1, MotionEvent e2,
                         float velocityX, float velocityY) {
      try {

        // Left swipe
        if ( velocityX < -SWIPE_THRESHOLD_VELOCITY) {
            if( velocityY < -SWIPE_THRESHOLD_VELOCITY) {
                GameWindow.writeToOutput("northwest");
                Log.d("Console","Wrote NW");
            }
            else
                GameWindow.writeToOutput("west");

            return true;
        // Right swipe
        } else if (velocityX > SWIPE_THRESHOLD_VELOCITY) {

            if( velocityY < -SWIPE_THRESHOLD_VELOCITY) {
                GameWindow.writeToOutput("northeast");
                Log.d("Console","Wrote NE");
            }
            else
                GameWindow.writeToOutput("east");

            return true;

        }

        if ( velocityY > SWIPE_THRESHOLD_VELOCITY) {

            GameWindow.writeToOutput("south");

            return true;
        }


        if ( velocityY < -SWIPE_THRESHOLD_VELOCITY) {

            GameWindow.writeToOutput("north");

            return true;
        }
      } catch (Exception e) {
        Log.e("YourActivity", "Error on gestures");
      }

      return false;
    }

My issue has been that, I will do an up-left "fling", but the velocities will instead suddenly display in the logcat that I did a "fling" in the opposite direction. Could this be an emulator issue, or is my code not accurately measuring the direction of my gesture?

Was it helpful?

Solution

Ok, Dutt was right and I would like to give him credit for this. I modified my code to measure the gestures a little more accurately and in case anyone would like to use this code, it's below. I'm going to have to modify the sensitivity a little bit since up-left is not going to have as high numbers as a straight up, but this is a really solid starting point.

    public boolean onFling(MotionEvent e1, MotionEvent e2,
                         float velocityX, float velocityY) {
      try {
          double xDir = e1.getX() - e2.getX();
          double yDir = e1.getY() - e2.getY();

          Log.d("Console","xVel = " + xDir);
          Log.d("Console","yVel = " + yDir);

        if ( xDir > SWIPE_THRESHOLD_VELOCITY) {
            if( yDir > SWIPE_THRESHOLD_VELOCITY) {
                //Up-Left swipe
                GameWindow.writeToOutput("northwest");
            }
            else  if ( yDir < -SWIPE_THRESHOLD_VELOCITY){
                //Down-Left swipe
                GameWindow.writeToOutput("southwest");
            }
            else{
                //Left swipe
                GameWindow.writeToOutput("west");
            }

            return true;
        } else if (xDir < -SWIPE_THRESHOLD_VELOCITY) {

            if( yDir > SWIPE_THRESHOLD_VELOCITY) {
                //Up-Right swipe
                GameWindow.writeToOutput("northeast");
            }else  if ( yDir < -SWIPE_THRESHOLD_VELOCITY){
                //Down-Right swipe
                GameWindow.writeToOutput("southeast");
            }
            else {
                //Right swipe
                GameWindow.writeToOutput("east");
            }

            return true;

        }

        if ( yDir < -SWIPE_THRESHOLD_VELOCITY) {
            //Down swipe
            GameWindow.writeToOutput("south");
            return true;
        }


        if ( yDir > SWIPE_THRESHOLD_VELOCITY) {
            //Up swipe
            GameWindow.writeToOutput("north");
            return true;
        }
      } catch (Exception e) {
        Log.e("YourActivity", "Error on gestures");
      }

      return false;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top