双击Cocos2d Android JAVA

时间:2015-03-19 20:42:54

标签: java android cocos2d-iphone sequence

尝试在Android中检测doubletap(Cocos2d Framework)。我做错了什么?

在ccTouchesEnded我有:

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!");
        }
    }

我已经有了重置器:

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

我的输出表明该序列根本没有运行..所以计数只是添加,200毫秒后没有重置...... :(

1 个答案:

答案 0 :(得分:0)

作为解决方案,我决定使用android自己的Handler类;

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);
        }

这是做什么的:通过计算点击次数并在第一次点击后200 ms将该计数重置为零来检测双击。