我正在嘗試使用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。
我將不勝感激任何幫助!謝謝!
你的代碼具有很高的可讀性......保持它:)。 – YvesLeBorg 2013-04-05 20:07:17
'xept一件事:在objc中它是BOOL不bool和YES/NO而不是TRUE/FALSE;) – LearnCocos2D 2013-04-05 20:17:01