ccTouchBegan函数正在执行If-Statement错误

时间:2013-04-05 19:46:30

标签: iphone cocos2d-iphone

我目前正在尝试使用Cocos2D在iOS设备上阅读和存储触摸。我的方法是在ccTouchBegan,ccTouchMoved,ccTouchCanceled和ccTouchEnded函数中单独处理每次触摸(启用多点触控)。

重要的ivars是:

  • activeTouches(int,当收到新的有效触摸(有效意义不在操纵杆附近)时递增或递减)。
  • 接受用户(bool,用于忽略任何新的触摸)
  • gestureTimer(在更新函数中由ccTime连续递增的float。当它高于某个阈值时,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功能中遇到了一个奇怪的问题。我的测试场景,使用模拟器,我有1次触摸软件识别,然后gestureTimer正在运行。在x时间之后,gestureTimer超过阈值并将acceptingGestures设置为FALSE。然后我在屏幕上应用第二次触摸,代码接受触摸并递增activeTouches变量!它应该不能这样做!

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

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

Debugger screenshot. Notice the acceptingGestures is FALSE.

1 个答案:

答案 0 :(得分:2)

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