2
votes

So I'm trying to learn SpriteKit while building what I think is a simple puzzle game. I have a 5x5 grid of SKSpriteNodes of different colors. What I want is to be able to touch one, and move my finger horizontally or vertically and detect all the nodes that my finger is touching, like if I was "selecting" them.

I tried to do something like this, but it crashes the app:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKSpriteNode *node = [self nodeAtPoint:location];

    NSLog(@"Dragged over: %@", node);
}

Is there something like a "touchEnter" / "touchLeave" kinda event that I'm missing? Sorry, I don't even know what I don't know.

1

1 Answers

3
votes

UIPanGestureRecognizer is your friend:

 -(void)didMoveToView:(SKView*)view {
    UIPanGestureRecognizer *recognizer = [[UIPangestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
    recognizer.delegate = self;
    [self.view addGestureRecognizer:recognizer];
 }
 -(void)hadlePangesture:(UIPanGestureRecognizer*)recognizer {
    CGPoint location = [recognizer locationInView:self.view];
    SKSpriteNode *node = [self nodeAtPoint:[self convertPointFromView:location]];
    if (node) {
        NSLog(@"Dragged over: %@", node);
    }
 }