2012-11-27 6 views
3

어떻게이 cocos2d-iphone 코드를 cocos2d-x로 이식합니까?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;  
    } 
} 

답변

7

이 구현 할 기능이 없다, 우리가 시험에 의해 두 clicks.In 총장의 틱이를 구현할 수 있습니다 더블 클릭의 시간 간격은 250ms ~ 300ms.Use이 얻을 수있는 시스템의 현재 숫자 사이 밀리 세컨드의

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

처럼 할 수있는이 어디서나 호출되거나 우리는 단지 그것을 구현합니까? – user1676682

0

는 선언

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

}