2014-02-22 61 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 */ 
}