2013-04-22 84 views
1

有什么方法可以限制用户手势的持续时间吗?例如,用户可以拖动一个精灵,但从cctouch开始,它只能持续3秒。持续时间过后,应用程序将自动触发cctouch结束方法。如何限制触摸持续时间?

回答

2

我会建议使用块调度计时器。避免在Cocos2D中使用NSTimer,因为它不允许使用内置的暂停/恢复功能。

时间表如下:

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    [[self scheduler] scheduleBlockForKey:@"menu" target:self interval:3.0f repeat:0 delay:0 paused:NO block:^(ccTime dt) 
      { 
       // perform end of touch actions here 
      }]; 
} 

还可以肯定,如果用户做了你想要的任何计时器被调用之前取消预定的块(ccTouchEnded/ccTouchCancelled很可能):

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    [[self scheduler] unscheduleBlockForKey:@"menu" target:self]; 
} 
+0

尼斯回答,我是新来的科科斯,我不知道有关NSTimers。谢谢 – 2013-04-22 18:21:04

2

是的,这是一个简单的策略来实现这一目标。您可以在用户开始实现手势时启动计时器,并在计时器命中时停止计时。

-(void) timerDidTick:(NSTimer *)theTimer{ 
    cpMouseRelease(mouse); 
} 

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 
    NSTimer *aTimer = [NSTimer timerWithTimeInterval:3.0 target:self selector:@selector(timerDidTick:) userInfo:nil repeats:NO] ; 
    [[NSRunLoop mainRunLoop] addTimer:aTimer forMode:NSRunLoopCommonModes]; 
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch]; 
    cpMouseGrab(mouse, touchLocation, false); 
... 
}