1

我创建了一个 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 名称。

4

2 回答 2

2

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

于 2014-03-05T07:19:27.677 回答
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返回值复杂。您可以查看文档以找到不同的情况

于 2014-03-05T07:51:15.023 回答