0

I need to add several swipe gestures to my scene on a game. From what I know, this can only be done programmatically, unfortunately I have never added gestures in this way, I have always used IB. I know I need to initialize a gesture recognizer, using initWithTarget:action: and I know how to set its properties, what I don't know is how to make this gesture do stuff. I assume it is through the action parameter @selector but this seems to never get called. Am I doing this wrong? here is what I have:

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                        action:@selector(animateSwipeRightLeft)];
[self.view addGestureRecognizer:swipeRight];

and for the selector I have:

-(void)animateSwipeRightLeft {

    //do stuff...
}

so it comes down to a few related questions: am I setting this up correctly? if so, why is my selector not being called? and if I am wrong about not being able to do this with IB, how?

also, if it helps, my gestures are set up in the initWithSize method for my scene.

  • check that self.view isn't nil at the time you are adding the recognizer. It will be nil in the init method of a node. – CodeSmile Jun 18 '14 at 22:15
  • @LearnCocos2D it is nil, so how do I fix it? –  Jun 19 '14 at 01:15
  • add the gesture recognizer after init, or where you create the node that currently contains the addgesturerecognizer code – CodeSmile Jun 19 '14 at 07:31

1 Answers1

0

You should add gesture recognizers in SKScene's didMoveToView method.

- (void)didMoveToView:(SKView *)view {
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                    action:@selector(animateSwipeRightLeft)];
    [self.view addGestureRecognizer:swipeRight];
}

self.view is nil inside init methods

Andrey Gordeev
  • 30,606
  • 13
  • 135
  • 162