2014-02-22 204 views
2

我有一个像UIButton一样的节点和一个在屏幕被触摸时执行动作的节点。 问题是,狮子跳跃行动没有被解雇。另外:两个节点的名称都是正确的。在UIView上检测触摸

我对的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 
     } 
    } 
+0

你登录查看任一节点是否满足isEqualToString条件? – AndyOS

+0

是的。当我触摸附近的狮子节点时它会发射。 –

+0

你确定lionIsJumping是假的吗?你能发布更多的代码吗?该操作以及布尔值设置为false的位置 – AndyOS

回答

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