2011-09-13 32 views
0

這個問題太原始,但我一直在努力的最後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]; 


} 
+0

你能後的handleSingleTouch:方法 – KDaker

回答

1

我不知道這是否只是在你的問題的錯字,但你的延遲方法是錯誤的。沒有withDelay:參數。它應該是這樣的:

[self performSelector:@selector(handleSingleTouch:) withObject:touch afterDelay:0.1]; 

編輯

由於您的問題是最有可能與觸摸迷路。在調用延遲方法之前,嘗試[touch retain]。另一種方法是將handleSingleTouch:方法更改爲採用兩個浮點數x和y或CCPoint作爲參數而不是UITouch。然後在延遲方法之前創建浮點數,並在延遲方法中傳遞它們。這樣你就可以避免內存泄漏,因爲很可能你在調用延遲方法後無法釋放它。

希望這有助於

+0

是的,這是問題的一個錯字?我現在糾正了它。 –

+0

您是否在調用延遲方法之前嘗試保留觸摸?我沒有看到你的代碼中的精靈被添加了,但是如果它取決於觸摸的值,那麼在延遲的方法被調用之前,觸摸可能被釋放。 – KDaker

+0

我想,你可能會做些什麼。我從來沒想過那個。會嘗試讓你知道。 –