ccTouchBegan函数执行If语句错误

问题描述:

我正在尝试使用Cocos2D在iOS设备上读取和存储触摸。我的方法是在ccTouchBegan,ccTouchMoved,ccTouchCanceled和ccTouchEnded函数中分别处理每个触摸(启用多点触控)。ccTouchBegan函数执行If语句错误

重要的是高德:

  • activeTouches(INT时递增或当接收到一个新的有效触摸(有效期,这意味着不能靠近操纵杆)递减)。
  • acceptingGestures(布尔其用于忽略任何新触摸)
  • gestureTimer(浮子被连续通过ccTime在更新函数递增。当它高于某个阈值,acceptingGestures被设置为FALSE)

这里是我的代码:

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    CGPoint touchLocation = [touch locationInView: [touch view]]; 
    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation]; 
    touchLocation = [self convertToNodeSpace:touchLocation]; 

    //check to see if touch is near joystick to prevent accidental gestures. may want to consider increasing the 'deadzone' value 
    if(abs(self.joystickLocation.x - touchLocation.x) <= 35 && abs(self.joystickLocation.y - touchLocation.y) <= 35) 
    { 
     return NO; 
    } 

    if(self.activeTouches == 0) 
    { 
     [self setGestureTimerEnable:TRUE]; 
     [self setGestureTimer:0.0f]; 
    } 

    if(self.acceptingGestures == TRUE); 
    { 
     if (self.firstTouch == nil) 
     { 
      self.firstTouch = touch; 
      self.activeTouches++; 
      return YES; 
     } 
     else if (self.secondTouch == nil) 
     { 
      self.secondTouch = touch; 
      self.activeTouches++; 
      return YES; 
     } 
    } 

    CCLOG(@"touched: %f, %f\n",touchLocation.x, touchLocation.y); 
    return NO; 
} 

我遇到的ccTouchBegan功能一个奇怪的问题。我的测试场景,使用模拟器,我有一个触摸软件识别,然后gestureTimer正在运行。在x时间之后,gestureTimer超过阈值并将acceptingGestures设置为FALSE。然后,我在屏幕上应用第二次触摸,代码接受触摸并增加activeTouches变量!它不应该能够这样做!

我使用调试器设置了一个断点,并尝试捕获这个奇怪的事件。尽管acceptingGestures是FALSE(断点处的acceptingGestures表达式确实是FALSE),但代码仍然通过If语句!在附加的屏幕截图中,请注意acceptingGestures为FALSE。

我将不胜感激任何帮助!谢谢!

Debugger screenshot. Notice the acceptingGestures is FALSE.

我发现我的问题。我的if语句后有一个分号! Womp womp。我想删除我的帖子,但也许有人会看到我的手势代码,并从中获得一些东西?

+0

你的代码具有很高的可读性......保持它:)。 – YvesLeBorg 2013-04-05 20:07:17

+0

'xept一件事:在objc中它是BOOL不bool和YES/NO而不是TRUE/FALSE;) – LearnCocos2D 2013-04-05 20:17:01