0

I have a spriteNode where I need to have swipe detection on a SKSpriteNode, but it seems that swipe gesture can only do on view. Is there a way to do this?

SKSpriteNode *rabbit = [SKSpriteNode spriteNodeWithImageNamed:@"rabbit_img"];

UISwipeGestureRecognizer * swipeleft=[[UISwipeGestureRecognizer alloc]initWithTarget:self   action:@selector(handleSwipeGesture:)];
swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeleft];

1 Answer 1

0

You can get the starting coordinates of the swipe via the state property.

- (void)swipe:(UISwipeGestureRecognizer *)recognizer
{
    CGPoint point = [recognizer locationInView:[recognizer view]];
    if (recognizer.state == UIGestureRecognizerStateBegan)
        NSLog(@"start coordinates: %@", NSStringFromCGPoint(point));
}

You can then compare those coordinates against your desired nodes using the SKNode method containsPoint: to see if the swipe originated from a node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.