【问题标题】:Double tap Cocos2d Android JAVA双击 Cocos2d Android JAVA
【发布时间】:2015-03-19 20:42:53
【问题描述】:

尝试检测 Android 中的双击(Cocos2d 框架)。我做错了什么?

在 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 毫秒后没有重置... :(

【问题讨论】:

  • event 中的某处可能嵌入了一个 tapcount 属性。在 touchesBegan 中抓住它。

标签: java android cocos2d-iphone sequence


【解决方案1】:

作为解决方案,我决定使用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 毫秒。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 2015-04-24
    • 1970-01-01
    • 2013-07-10
    相关资源
    最近更新 更多