在SpriteKit中,“选择”我的手指在移动时触摸的所有精灵

时间:2013-11-27 05:39:33

标签: ios sprite-kit

所以我正在尝试学习SpriteKit,同时构建我认为简单的益智游戏。我有一个5x5网格的不同颜色的SKSpriteNodes。我想要的是能够触摸一个,水平或垂直移动我的手指,并检测我的手指触摸的所有节点,就像我“选择”它们一样。

我试图做这样的事情,但它崩溃了应用程序:

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

是否有类似“touchEnter”/“touchLeave”之类的事件,我错过了?对不起,我甚至不知道我不知道的事情。

1 个答案:

答案 0 :(得分:3)

UIPanGestureRecognizer是你的朋友:

 -(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);
    }
 }
相关问题