Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when UIGestureRecognizer is up, down, left and right Cocos2d

I have a CCSprite that I want to move around using gestures. Problem is I'm completely new to Cocos2D. I want my sprite to perform one action when the gesture is up, another one when gesture is down, another action when gesture is right and same thing for left. Can someone point me in the right direction?

Thanks!

like image 283
Magnus Avatar asked Sep 14 '11 16:09

Magnus


2 Answers

Apparently each UISwipeGestureRecognizer can only detect the swipe in the given direction. Even though the direction flags could be OR'ed together the UISwipeGestureRecognizer ignores the additional flags.

The solution is to add one UISwipeGestureRecognizer for each direction you want the swipe gesture to be recognized, and set each recognizer's direction accordingly to either up, down, left and right. If you want to test for a swipe in any direction you'll have to add four UISwipeGestureRecognizers.

It's kind of odd but that's the only way it worked for me.

like image 73
LearnCocos2D Avatar answered Oct 20 '22 23:10

LearnCocos2D


UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)]; swipeGesture.direction = UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown; [self.gestureAreaView addGestureRecognizer:swipeGesture]; [swipeGesture release];  -(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender  {     //Gesture detect - swipe up/down , can't be recognized direction }  or  UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)]; swipeGesture.direction = UISwipeGestureRecognizerDirectionUp; [self.view addGestureRecognizer:swipeGesture]; [swipeGesture release];  UISwipeGestureRecognizer *swipeGesture2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)]; swipeGesture2.direction = UISwipeGestureRecognizerDirectionDown; [self.view addGestureRecognizer:swipeGesture2]; [swipeGesture2 release];  -(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender  {     //Gesture detect - swipe up/down , can be recognized direction     if(sender.direction == UISwipeGestureRecognizerDirectionUp)     {     }     else if(sender.direction == UISwipeGestureRecognizerDirectionDown)     {     } } 
like image 39
DevGom Avatar answered Oct 20 '22 23:10

DevGom