1

In the photo below, each triangle is a separate subclassed SKShapeNode. How do you recognize which triangle is touched? Currently in the scene's toucheBegan: method, touching the grid detects two triangles since their frame's are square.

enter image description here

Ivan Lesko
  • 1,274
  • 13
  • 26
  • You'll have to calculate the touch's enclosing shape yourself, taking into account the triangular shape. [How to determine a point in a triangle?](http://stackoverflow.com/q/2049582) – jscs Apr 14 '14 at 04:56

1 Answers1

2

I managed to solve this problem by setting the UIBezierPath path that draws the triangle as a property on the Triangle class. In the scene's toucheBegan: method, I check if the touch is contained within the Triangle's touchableArea UIBezierPath property.

- (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.
            }
        }
    }
}
Ivan Lesko
  • 1,274
  • 13
  • 26