Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sprite kit 如何检测是否触摸了SKShapeNode?_Sprite Kit_Sknode_Skshapenode - Fatal编程技术网

Sprite kit 如何检测是否触摸了SKShapeNode?

Sprite kit 如何检测是否触摸了SKShapeNode?,sprite-kit,sknode,skshapenode,Sprite Kit,Sknode,Skshapenode,我创造了一个sknodeshape。如何检测我制作的形状是否被触摸(单击) 下面是代码:(我已经解决了) 我的错误是,当我创建形状时,我在初始化它之前输入了SKShapeNode名称。实现touch-delegate方法。然后,在-touchesbeated:方法中,提取接触点并使用[self nodeAtPoint:]方法检索节点 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { /* Called wh

我创造了一个sknodeshape。如何检测我制作的形状是否被触摸(单击)

下面是代码:(我已经解决了)


我的错误是,当我创建形状时,我在初始化它之前输入了SKShapeNode名称。

实现touch-delegate方法。然后,在-touchesbeated:方法中,提取接触点并使用[self nodeAtPoint:]方法检索节点

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];

    SKNode *yourNode = ....;
    CGRect yourNodeFrame = bowlNode.frame;

    if (CGRectContainsPoint(yourNodeFrame, location)) {

        //your node may be touched, check if it's your node
        SKNode *theNode = [self nodeAtPoint:location];

        if ([theNode.name isEqualTo:yourNode.name]) {
            //it's your node touched
        }
    }
}

方法
nodeAtPoint
返回值很复杂。您可以检查文档以找到不同的情况

实际形状(路径)或其框架?您的解决方案肯定是错误的。不管怎样,只要使用我的代码,它就简单多了。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];

    SKNode *yourNode = ....;
    CGRect yourNodeFrame = bowlNode.frame;

    if (CGRectContainsPoint(yourNodeFrame, location)) {

        //your node may be touched, check if it's your node
        SKNode *theNode = [self nodeAtPoint:location];

        if ([theNode.name isEqualTo:yourNode.name]) {
            //it's your node touched
        }
    }
}