這個問題太原始,但我一直在努力的最後8小時,它正在蠶食我的能量(和置信水平太:))處理槍王在cocos2d
在我的課,我已經註冊爲有針對性的觸摸。
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-1 swallowsTouches:YES];
我想檢查用戶是單擊還是雙擊。在ccTouchBegan方法中,首先與singleTap進行聯繫,然後使用doubleTap進行聯繫。我在其中一個Cocos2d論壇上發現了一個非常有趣的解決方案。 (我現在找不到它..)
解決方案是這樣的。
switch (touch.tapCount) {
case 2:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(handleSingleTouch:) object:singleTouchLocation];
// Handle Double Touch here
break;
case 1:
self.singleTapLocation = ccp(location.x,location.y);
self.singleTouchLocation = touch;
[self performSelector:@selector(handleSingleTouch:) withObject:touch afterDelay:0.3
];
break;
}
一切看起來工作正常。本質上,你安排一個延遲的單觸摸處理程序,看看它實際上是一個雙擊。如果是雙擊,則取消singletap處理程序並執行雙擊的邏輯。
但我的問題是,在單觸處理程序(handleSingleTouch)中,我添加了一個CCSprite到UI。此操作不起作用。沒有精靈被添加。 (雖然該方法被稱爲。)。實際上,這是有效的,如果我毫不拖延地調用選擇器,但延遲,則不會添加該精靈。
我不是Objective C的專家,所以,如果問題太原始,我很抱歉。
編輯1:原來的thread。
編輯2:張貼handleSingleTouch ..只有相關的代碼。
-(void) handleSingleTouch:(UITouch *) touch {
CGPoint location = [touch locationInView: [touch view]];
CGPoint glLocation = [MainSceneLayer locationFromTouch:touch];
//Single tap
CCLOG(@"Single tap: Adding marker image");
if(zoomedOut) {
CGPoint globalCoordinates = [quadConvertor convertLocalToGlobal:location
inActiveQuadrant:activeQuadrant];
if([self isTouchValidForSelectedItem:globalCoordinates] == Matched) {
[self addMarkerImages:glLocation];
} else if([self isTouchValidForSelectedItem:globalCoordinates] == NotMatched) {
[missedLabel stopAllActions];
for (ImageQuadrants* quad in quadrants) {
if(quad.quadrantNumber == activeQuadrant) {
missedLabel.position = ccp((quad.center.x * -1)+glLocation.x , (quad.center.y * -1)+glLocation.y);
//markers.position = ccp((quad.center.x * -1)+touchPosition.x , 320);
}
}
id blinkAction = [CCBlink actionWithDuration:1 blinks:3];
id makeInvible = [CCCallFunc actionWithTarget:self selector:@selector(makeInvisible:)];
id seq = [CCSequence actions:blinkAction, makeInvible, nil];
[missedLabel runAction:seq];
} else {
[alreadyAccountedLabel stopAllActions];
for (ImageQuadrants* quad in quadrants) {
if(quad.quadrantNumber == activeQuadrant) {
alreadyAccountedLabel.position = ccp((quad.center.x * -1)+glLocation.x , (quad.center.y * -1)+glLocation.y);
//markers.position = ccp((quad.center.x * -1)+touchPosition.x , 320);
}
}
id blinkAction = [CCBlink actionWithDuration:1 blinks:3];
id makeInvible = [CCCallFunc actionWithTarget:self selector:@selector(makeInvisible1:)];
id seq = [CCSequence actions:blinkAction, makeInvible, nil];
[alreadyAccountedLabel runAction:seq];
}
}
swipeStartPoint = [touch locationInView:touch.view];
}
你能後的handleSingleTouch:方法 – KDaker