How can you detect if SKShapeNode is touched?

旧城冷巷雨未停 提交于 2019-12-08 06:13:07

问题


I created a sknodeshape. How can I detect if the shape I made was touched(clicked)?

Here is the code: (I solved it already)

//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");
}

my mistake was when I created the shape I put the SKShapeNode name before initializing it.


回答1:


Implement the touch delegate methods. Then, in the -touchesBegan: method, extract the point of touch and retrieve the node using the [self nodeAtPoint:] method




回答2:


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

the method nodeAtPoint return value is complicated. you can check the document to find different situations



来源:https://stackoverflow.com/questions/22191077/how-can-you-detect-if-skshapenode-is-touched

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