问题
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