2014-04-14 31 views
1

在下面的照片中,每个三角形都是一个单独的子类SKShapeNode。你如何识别触摸了哪个三角形?目前在场景的toucheBegan:方法中,触摸网格会检测两个三角形,因为它们的框架是方形的。检测按钮的三角形网格中的触摸?

enter image description here

+0

考虑到三角形形状,您必须自己计算触摸的封闭形状。 [如何确定三角形中的点?](http://stackoverflow.com/q/2049582) –

回答

2

我设法通过设置绘制三角形的三角类属性的UIBezierPath路径来解决这个问题。在场景的toucheBegan:方法中,我检查触摸是否包含在Triangle的touchableArea UIBezierPath属性中。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint position = [touch locationInNode:self]; 
    NSArray *nodes = [self nodesAtPoint:position]; 
    for (TrianglePiece *triangle in nodes) { 
     if ([triangle isKindOfClass:[TrianglePiece class]]) { 
      position = [touch locationInNode:triangle]; 
      if ([triangle.touchableArea containsPoint:position]) { 
       // Perform logic here. 
      } 
     } 
    } 
}