2015-05-19 137 views
4

嗨,我試圖用手勢識別與我的精靈套件的遊戲,我寫了這個代碼SpriteKit手勢識別器

@interface GameScene() <UIGestureRecognizerDelegate>{ 

    UISwipeGestureRecognizer *swipeGestureLeft; 
    ISwipeGestureRecognizer *swipeGestureRight; 
} 
@end 

@implementation GameScene 
-(id)initWithSize:(CGSize)size{ 

    if(self = [ super initWithSize:size]){ 

    } 

    return self; 
} 

-(void)didMoveToView:(SKView *)view{ 

    swipeGestureLeft = [[UIGestureRecognizer alloc]initWithTarget:self action:@selector(swipeLeft)]; 
    [swipeGestureLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; 
    [view addGestureRecognizer:swipeGestureLeft]; 

    swipeGestureRight = [[UIGestureRecognizer alloc]initWithTarget:self action:@selector(swipeRight)]; 
    [swipeGestureRight setDirection:UISwipeGestureRecognizerDirectionRight]; 
    [view addGestureRecognizer:swipeGestureRight]; 
} 

- (void) willMoveFromView: (SKView *) view { 

    [view removeGestureRecognizer: swipeGestureLeft ]; 
    [view removeGestureRecognizer: swipeGestureRight]; 
} 

-(void)swipeLeft:(UISwipeGestureRecognizer*) recognizer{ 

    [email protected]"Left"' 
} 

-(void)swipeRight:(UISwipeGestureRecognizer*) recognizer{ 

    [email protected]"Right"' 
} 


@end 

我覺得每一件事情是確定的,但我的手勢將無法正常工作,我不有錯誤信息,是否有東西丟失我應該添加到我的應用程序,或者我應該添加一些東西到我的視圖控制器,或者你們可以建議我一個totorial顯示我如何使用手勢與sprite套件,

+0

你爲什麼要刪除GestureRecognizer?這是必要的嗎? – LinusGeffarth

+1

@LinusG。我猜是因爲場景過渡。那些swipeRight和swipeLeft方法在​​該場景中定義。在場景轉換後識別器不會被刪除,所以應用會崩潰。 – Whirlwind

回答

5

試試這個代碼,它適用於我。你有一個錯字,我猜...

@interface GameScene() <UIGestureRecognizerDelegate>{ 

     UISwipeGestureRecognizer *swipeGestureLeft; 
     UISwipeGestureRecognizer *swipeGestureRight; 
     UITapGestureRecognizer *doubleTapGesture; 
    } 

    @end 

    @implementation GameScene 

    -(void)didMoveToView:(SKView *)view { 
     /* Setup your scene here */ 

     //You should use UISwipeGestureRecognizer instead of UIGestureRecognizer here. 

     swipeGestureLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeLeft:)]; 
     [swipeGestureLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; 
     [view addGestureRecognizer:swipeGestureLeft]; 


     //Note that swipeRight has a parameter, so you have to change swipeRight to swipeRight: to silent the compiler warning. 
     swipeGestureRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeRight:)]; 
     [swipeGestureRight setDirection:UISwipeGestureRecognizerDirectionRight]; 
     [view addGestureRecognizer:swipeGestureRight]; 


     //double tap detection 
     doubleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapTap:)]; 
     [doubleTapGesture setNumberOfTapsRequired:2]; 
     [view addGestureRecognizer:doubleTapGesture]; 
    } 

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
     /* Called when a touch begins */ 


    } 

    -(void)update:(CFTimeInterval)currentTime { 
     /* Called before each frame is rendered */ 
    } 

    - (void) willMoveFromView: (SKView *) view { 


     [view removeGestureRecognizer: swipeGestureLeft ]; 

     [view removeGestureRecognizer: swipeGestureRight]; 

     [view removeGestureRecognizer: doubleTapGesture]; 
    } 

    -(void)swipeLeft:(UISwipeGestureRecognizer*) recognizer{ 

     NSLog(@"Left"); 

    } 

    -(void)swipeRight:(UISwipeGestureRecognizer*) recognizer{ 

     NSLog(@"Right"); 


    } 

    -(void)tapTap:(UITapGestureRecognizer*) recognizer{ 

     NSLog(@"Tap tap"); 

    } 

    @end 
+0

非常感謝你,手勢效果很好,所以我認爲如果我使用輕擊手勢識別器作爲我用於輕掃手勢的相同方式沒有問題? – Omar

+0

歡迎您,很高興幫助您...您可以用這種方式在SKView上使用任何手勢識別器。 – Whirlwind

+0

@Omar再次查看代碼,我添加了一個輕擊識別器的示例。 – Whirlwind