2011-02-13 228 views
9

有沒有人設法讓手勢識別在cocos-2d中工作?cocos2d-iOS - 手勢識別器

我在這裏讀了後聲稱已經實現了,在這裏:http://www.cocos2d-iphone.org/forum/topic/8929

我從git的樞紐在這裏打補丁:https://github.com/xemus/cocos2d-GestureRecognizers/blob/master/README

我做了CCSprite一個子類(這是CCNode一個子類):

-(id) initWithTexture:(CCTexture2D*)texture rect:(CGRect)rect { 
if((self=[super initWithTexture:texture rect:rect])) 
{ 
    CCGestureRecognizer* recognizer; 
    recognizer = [CCGestureRecognizer 
     CCRecognizerWithRecognizerTargetAction:[[[UITapGestureRecognizer alloc]init] autorelease] 
        target:self 
        action:@selector(tap:node:)]; 
    [self addGestureRecognizer:recognizer]; 
} 
return self; 
} 

委託方法:

- (void) swipe:(UIGestureRecognizer*)recognizer node:(CCNode*)node 
{ 
NSLog(@" I never get called :("); 
} 

我的龍頭事件永遠不會被調用。

這個有用嗎?手動識別手勢進行手勢識別有多困難?

回答

24

您需要將手勢識別器附加到「上鍊」。不要將它們附加到單個節點上;將它們附加到UIView(即[[CCDirector sharedDirector] openGLView])。

這裏就是我所做的:

- (UIPanGestureRecognizer *)watchForPan:(SEL)selector number:(int)tapsRequired { 
    UIPanGestureRecognizer *recognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:selector] autorelease]; 
    recognizer.minimumNumberOfTouches = tapsRequired; 
    [[[CCDirector sharedDirector] openGLView] addGestureRecognizer:recognizer]; 
    return recognizer; 
} 

- (void)unwatch:(UIGestureRecognizer *)gr { 
    [[[CCDirector sharedDirector] openGLView] removeGestureRecognizer:gr]; 
} 

這個特殊的代碼是在場景控制器的超使用,因此對於選擇的目標是硬編碼爲「自我」,但你可以很容易地抽象,要一個傳入的對象。此外,您可以推斷上面輕鬆地創建爲龍頭,捏手勢識別等

在子類控制器,那麼,我只是這樣做:

- (MyController *)init { 
    if ((self = [super init])) { 
     [self watchForPan:@selector(panning:) number:1]; 
    } 
    return self; 
} 

- (void)panning:(UIPanGestureRecognizer *)recognizer { 

    CGPoint p; 
    CGPoint v; 

    switch(recognizer.state) { 
     case UIGestureRecognizerStatePossible: 
     case UIGestureRecognizerStateBegan: 
      p = [recognizer locationInView:[CCDirector sharedDirector].openGLView]; 
      (do something when the pan begins) 
      break; 
     case UIGestureRecognizerStateChanged: 
      p = [recognizer locationInView:[CCDirector sharedDirector].openGLView]; 
      (do something while the pan is in progress) 
      break; 
     case UIGestureRecognizerStateFailed: 
      break; 
     case UIGestureRecognizerStateEnded: 
     case UIGestureRecognizerStateCancelled: 
      (do something when the pan ends) 
      (the below gets the velocity; good for letting player "fling" things) 
      v = [recognizer velocityInView:[CCDirector sharedDirector].openGLView]; 
      break; 
    } 

} 
+0

+1對於cc的回答以上,這是正確的方法。您可以用這種方式使用所有UIGestureRecognizer子類來檢測平底鍋,捏,輕敲手勢。您還可以使用UILongPressGestureRecognizer和狀態大小寫UIGestureRecognizerStateChanged來識別拖動。 – KomodoDave 2011-04-24 02:02:00

+6

不錯,乾淨,可愛。兩個小問題:1. locationInView返回UIKit座標系中的座標,這些座標系在使用前應轉換爲GL座標,例如:p = [[CCDirector sharedDirector] convertToGL:[recognitionizer locationInView:[CCDirector sharedDirector] .view]] ;` 2. openGLView自Cocos2D 2.0開始棄用,它已被`[CCDirector sharedDirector] .view`取代。 – Eliot 2012-07-26 01:40:28