Touch detection in a triangular button grid?

In the following figure, each triangle represents a separate subclass of SKShapeNode. How do you know which triangle is touched? Currently on the scene is touchheBegan: the method, by touching the grid, detects two triangles, since their frame is square.

enter image description here

+1
source share
1 answer

I managed to solve this problem by setting the path UIBezierPath, which draws a triangle as a property in the Triangle class. In the touchheBegan: method scene, I check to see if the touch is contained inside the Triangle property of 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.
            }
        }
    }
}
+2
source

Source: https://habr.com/ru/post/1530698/


All Articles