ins SpriteKit, “select” all the sprites my finger touches while moving

三世轮回 提交于 2019-12-11 03:59:55

问题


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:


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);
    }
 }


来源:https://stackoverflow.com/questions/20234718/ins-spritekit-select-all-the-sprites-my-finger-touches-while-moving

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!