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

3 回答 3

7

没有实现这个的函数,我们可以通过测试两次点击之间的tick来实现。一般来说,双击的时间间隔在250ms~300ms之间。用这个来获取系统当前的毫秒数

long millisecondNow()
{
    struct cc_timeval now;
    CCTime::gettimeofdayCocos2d(&now, NULL);
    return (now.tv_sec * 1000 + now.tv_sec / 1000);
}
于 2012-11-30T08:39:57.820 回答
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) 在设备上更准确。模拟器中存在延迟,因为间隔实际上不是挂钟时间。

于 2015-05-29T17:09:10.000 回答
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");
    };

}
于 2015-11-18T07:48:45.037 回答