2
votes

Trying to detect doubletap in Android (Cocos2d Framework). What am I doing wrong?

In ccTouchesEnded i have :

public boolean ccTouchesEnded(MotionEvent event) {

    touchTapCount++;
    Lg("Tapcount : " + touchTapCount);
    if (touchTapCount == 1) {
        Lg("We're in the 1 thingie!");
        CCDelayTime delayaction = CCDelayTime.action(0.2f);
        CCCallFunc callSelectorAction = CCCallFunc.action(this, "dtreset");
        CCSequence a = CCSequence.actions(delayaction,(CCFiniteTimeAction) callSelectorAction);
        this.runAction(a);
    } else {
        if (touchTapCount ==2){
            Lg("Oh yeah we got double tap!");
        }
    }

And I've got the resetter :

public void dtreset(Object Sender){
    Lg("Resetted the TouchTapCount");
    touchTapCount = 0;
}

My output indicates that the sequence is not runned at all.. So count just gets added, there is no reset after 200 ms... :(

1
There is probably a tapcount property embedded somewhere in event. Catch it in touchesBegan.YvesLeBorg

1 Answers

0
votes

As a solution, I decided to use android's own Handler class;

public boolean ccTouchesEnded(MotionEvent event) {

    touchTapCount++;
    Lg("Tapcount : " + touchTapCount);
    if (touchTapCount == 1) {
        // Very important bit of code..
        // First, we define a Handler and a Runnable to go with it..
        Handler handler = new Handler(Looper.getMainLooper());
        final Runnable r = new Runnable() {
            public void run() {
                // In the runnable, we set the touchTapCount back to 0..
                touchTapCount = 0;
            }
        };
        // Now, execute this handler with a delay of 200ms..
        handler.postDelayed(r, 200);

    } else {
        if (touchTapCount == 2){
            wasdoubletapped = true;
            verifySelectorTypeBeforeRotate(cC, cR, SELECTOR_CROSS);
        }

So what this does: detect double tap, by counting the number of taps and resetting that count to zero, 200 ms after the first tap.