jssmkp
jssmkp

Reputation: 271

How do you detect a double tap drag?

How do you detect a double tap drag? I am making a game similar to bejeweled, and would like to detect this. In my current implementation, I was able to get the swipe effects. I would also like to detect the double tap drag effect for a different functionality in the game.

CODE:

@Override
public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {

    if (e.getAction() == MotionEvent.ACTION_DOWN && board.touchBoard(scaledX, scaledY) ) {
        recentTouchY = scaledY;
        recentTouchX = scaledX;

        board.whichCell(scaledX,scaledY);

        touchedCell = board.whichCellTouched(scaledX,scaledY);
        //board.swapClearShape(touchedCell.getOffX(),touchedCell.getOffY());
    } else if (e.getAction() == MotionEvent.ACTION_UP) { //NEED TO ADD IF TOUCH BOARD AREA

        //swipe event
        if (scaledX - recentTouchX < -25) {

            System.out.println("SWAPPED LEFT");
            Assets.playSound(Assets.swapSound);
            board.setSwapLeft(true);

        }
        else if (scaledX - recentTouchX > 25) {
            System.out.println("SWAPPED RIGHT");
            Assets.playSound(Assets.swapSound);
            board.setSwapRight(true);

        }
        //swap down
        else if(scaledY- recentTouchY > 25){
            System.out.println("SWAPPED DOWN");
            Assets.playSound(Assets.swapSound);
            board.setSwapDown(true);

        }
        //swap up
        else if(scaledY- recentTouchY < -25){
            System.out.println("SWAPPED UP");
            Assets.playSound(Assets.swapSound);
            board.setSwapUp(true);

        }

    }
    return true;
}

I am not looking for two finger drag, rather one touch followed by and another touch then drag

Upvotes: 3

Views: 931

Answers (1)

paprikanotfound
paprikanotfound

Reputation: 342

You can use GestureDetector.OnDoubleTapListener to detect the double tap gesture and trigger a flag. Then on your onTouch (MotionEvent.ACTION_MOVE) do the drag logic.

If you use the onDoubleTapEvent(MotionEvent e) method, you are be able to detect the events between the double tap gesture and trigger the flag before the second ACTION_UP event.

Here's an example of the double tap listener implementation: http://developer.android.com/training/gestures/detector.html

Upvotes: 2

Related Questions