Detecting touches in a triangular grid of buttons?

不想你离开。 提交于 2019-12-23 04:32:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!