0

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?

Jon
  • 55
  • 9
  • It's a good basic assumption to start off with that the error is in your code, not the emulator. After checking your code and making sure it works you can start doubting the emulator. – dutt May 05 '13 at 06:27
  • So, I inserted some logcat lines to print out the velocities of X and Y, they seem to be rather erratic sometimes. An upward gesture, which should produce -velocityY behaves correctly sometimes, but others the value is suddenly positive, which is not what I'm wanting. Are there any ways to fix this with the emulator? I sent my .apk to a friend and he says it seems to behave as I expected on an actual device. – Jon May 05 '13 at 06:51

1 Answers1

2

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;
    }
Jon
  • 55
  • 9