检测三角形按钮网格中的触摸? [英] Detecting touches in a triangular grid of buttons?

查看:93
本文介绍了检测三角形按钮网格中的触摸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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.
            }
        }
    }
}

这篇关于检测三角形按钮网格中的触摸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆