如何检测SKShapeNode是否被触摸?

时间:2014-03-05 07:14:48

标签: sprite-kit sknode skshapenode

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

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

//metung_babi is the name of the SKShapeNode

UITouch *touch = [touches anyObject];
CGPoint nokarin = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:nokarin];

if ([node.name isEqualToString:@"metung_babi"]) {
    NSlog(@"touch me not");
}

我的错误是在创建形状时我在初始化之前放了SKShapeNode名称。

2 个答案:

答案 0 :(得分:2)

实施触摸委托方法。然后,在-touchesBegan:方法中,使用[self nodeAtPoint:]方法提取触摸点并检索节点

答案 1 :(得分:0)

-(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返回值很复杂。您可以查看文档以查找不同的情况