如何在cocos2d-x中检测双击事件

3
我该如何将这个cocos2d-iphone代码移植到cocos2d-x?
(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  {
 NSSet *allTouches = [event allTouches]; 
switch ([allTouches count])    {
case 1:  
    {
        UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
        switch([touch tapCount]) 
        { 
             case 1:
                  // 单击!
                 break;
             case 2:
                //Double tap.
               // 双击! 
                break;
        } 
   break;    
   } 
}
3个回答

7

目前没有实现此功能的函数,我们可以通过测试两次点击之间的滴答声来实现此功能。通常,双击的时间间隔在250ms~300ms之间。使用此方法获取系统当前的毫秒数。

long millisecondNow()
{
    struct cc_timeval now;
    CCTime::gettimeofdayCocos2d(&now, NULL);
    return (now.tv_sec * 1000 + now.tv_sec / 1000);
}

这个被调用了吗还是我们只是实现它? - user1676682

0
在MyScene.h中声明:
int tapCount;
Touch lastTouch;
void singleTap(float deltaTime);

然后在MyScene.cpp中:

bool MyScene::onTouchBegan(Touch* touch, Event* event)
{
    ++tapCount;
    lastTouch = *touch;

    if (tapCount == 1) {
        this->schedule(schedule_selector(MyScene::singleTap), 0.25, 1, 0);
    }
    else {
        this->unschedule(schedule_selector(MyScene::singleTap));
        tapCount = 0;
        printf("\n\ndouble tap\n\n");
    }

    return true;
}


void MyScene::singleTap(float deltaTime)
{
    this->unschedule(schedule_selector(LevelScene::singleTap));
    tapCount = 0;
    printf("\n\nsingle tap\n\n");
}

然后您可以在singleTap或doubleTap方法中访问lastTouch。如果您不需要lastTouch成为Touch对象,则可以使用Vec2并设置坐标。请注意,计时器间隔(0.25)在设备上更准确。模拟器中存在延迟,因为间隔实际上不是挂钟时间。


0
你可以像这样做
void HelloWorld::callback1()
{
    _tapCount = 0;
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
    _tapCount = _tapCount + 1;
    if (_tapCount == 1)
    {
        DelayTime* delayAction = DelayTime::create(0.3);
        CallFunc*resetAction = CallFunc::create(CC_CALLBACK_0(HelloWorld::callback1, this));
        Sequence *seq = Sequence::create(delayAction, resetAction, NULL);
        this->runAction(seq);
    }
    else{
        log("double tap");
    };

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接