我在_background(绿色)上添加了两个精灵,并试图通过将手指拖过它们来选择精灵。
[_background addChild:S1];
[_background addChild:S2];
我正在使用此问题中的方法:select all the sprites my finger touches while moving
我没有受到实际太空飞船精灵的影响,只有背景。
我使用的代码:
- (void)handlePan:(UIPanGestureRecognizer *)sender {
_touchLocation = [sender locationInView:sender.view];
_touchLocation = [self convertPointFromView:_touchLocation];
...
if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"UIGestureRecognizerStateBegan");
//////Make sure that no sprites are moved during the selecting phase//////
_background.userInteractionEnabled = NO;
} else if (sender.state == UIGestureRecognizerStateChanged) {
NSLog(@"UIGestureRecognizerStateChanged");
SKNode *node = [_background nodeAtPoint:[self convertPointFromView:_touchLocation]];
//if (![node.name isEqualToString:@"background"]) {
NSLog(@"Selected node:%@", node.name);
//}
} else if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"UIGestureRecognizerStateEnded");
_background.userInteractionEnabled = YES;
}
...
结果:
2014-01-05 21:38:34.011 xxxxx[21172:a0b] UIGestureRecognizerStateBegan
2014-01-05 21:38:36.513 xxxxx[21172:a0b] UIGestureRecognizerStateChanged
2014-01-05 21:38:36.514 xxxxx[21172:a0b] Selected node:background
2014-01-05 21:38:36.545 xxxxx[21172:a0b] UIGestureRecognizerStateChanged
2014-01-05 21:38:36.546 xxxxx[21172:a0b] Selected node:background
2014-01-05 21:38:36.578 xxxxx[21172:a0b] UIGestureRecognizerStateChanged
2014-01-05 21:38:36.579 xxxxx[21172:a0b] Selected node:background
…
2014-01-05 21:38:36.987 xxxxx[21172:a0b] UIGestureRecognizerStateEnded
当我把手指放在太空船上时,我无法击中太空船。
我如何创建UIPanGestureRecognizer
_panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
[self.view addGestureRecognizer:_panGesture];
答案 0 :(得分:0)
您可以在方法开头执行此操作:
_touchLocation = [self convertPointFromView:_touchLocation];
你以后再做一次:
SKNode *node = [_background nodeAtPoint:[self convertPointFromView:_touchLocation]];
我的猜测是将点转换两次可能会给你错误的位置。您应该记录点以确定它们是正确的。
如果不是这样,那么您可能需要将位置转换为船舶父节点空间。如下:
_touchLocation = [_background convertPoint:_touchLocation toNode:_background];