【问题标题】:Detect touch on UIView检测 UIView 上的触摸
【发布时间】:2014-02-22 17:03:14
【问题描述】:

我有一个类似于 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
        }
    }

【问题讨论】:

  • 您是否登录以查看任一节点是否满足 isEqualToString 条件?
  • 是的。当我触摸附近的 lionNode 时它会触发。
  • 你确定 lionIsJumping 是假的吗?您可以发布更多代码吗?布尔值设置为 false 的操作和位置

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


【解决方案1】:

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 */
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 2014-06-21
    相关资源
    最近更新 更多