问题
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.

回答1:
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.
}
}
}
}
来源:https://stackoverflow.com/questions/23051961/detecting-touches-in-a-triangular-grid-of-buttons