检测UIView上的触摸

时间:2014-02-22 17:03:15

标签: ios objective-c uiview touch sprite-kit

我有一个充当UIButton的节点和一个在触摸屏幕时执行操作的节点。 问题是狮子跳跃动作不会被解雇。 另外:节点的两个名称都是正确的。

我的UIEvent代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        CGPoint location = [touch locationInNode: self];
        SKNode *node = [self nodeAtPoint:location];

        UITouch *lionTouch = [touches anyObject];
        CGPoint locationLion = [lionTouch locationInView:[self view]];
        SKNode *lionNode = [self nodeAtPoint:locationLion];

        if ([lionNode.name isEqualToString:@"veggieLion"]) {
            if (!lionIsJumping) {
                [self lionJump];

                lionIsJumping = YES;
            }
        }

        if ([node.name isEqualToString:@"repalyButton"]) {
            // Button action
        }
    }

1 个答案:

答案 0 :(得分:0)

Apple documentation for SKNode表示方法nodeAtPoint返回与点相交的最深层后代。

过去我遇到过这个麻烦。该方法将返回我用于后台的SKSpriteNode,即使我肯定是在点击目标精灵/节点!

由于您正在检查两个节点,我将按以下方式处理它:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  UITouch *touch = [touches anyObject];
  CGPoint location = [touch locationInNode: self];

  SKNode * lionNode = [self childNodeWithName:@"veggieLion"];
  SKNode * replayButton = [self childNodeWithName:@"repalyButton"];

  if ([self.lionNode containsPoint:location]) {
    if (!lionIsJumping) {
      [self lionJump];
      lionIsJumping = YES;
    }
    return;
  }

  if ([self.replayButton containsPoint:location]) {
    // Button action
    return;
  }

  /* test for other targets */
}