我有一些像这样的图像中的 SKSpriteNode :
当我尝试像这样检测触摸时:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"shape"]) {
NSLog(@"TOUCH DETECT");
}
}
即使我触摸角落里的外部蓝色形状,它也会检测到触摸。它采用方形形状进行触摸检测。如何绕过它?我只想检测蓝色形状内的触摸。
马可
最佳答案
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"shapeMask"]) {
//Whatever you want.
}
}
-(void)blueShape{
SKSpriteNode *blueShape = [SKSpriteNode spriteNodeWithImageNamed:@"blueShape"];
blueShape.position = yourPosition;
blueShape.name = @"blueShape";
[self addChild:blueShape];
SKShapeNode *shapeMask = [SKShapeNode node];
shapeMask.name = @"shapeMask";
CGFloat offsetX = blueShape.frame.size.width * blueShape.anchorPoint.x;
CGFloat offsetY = blueShape.frame.size.height * blueShape.anchorPoint.y;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0 - offsetX, 137 - offsetY);
CGPathAddLineToPoint(path, NULL, 80 - offsetX, 1 - offsetY);
CGPathAddLineToPoint(path, NULL, 239 - offsetX, 2 - offsetY);
CGPathAddLineToPoint(path, NULL, 318 - offsetX, 139 - offsetY);
CGPathAddLineToPoint(path, NULL, 238 - offsetX, 275 - offsetY);
CGPathAddLineToPoint(path, NULL, 80 - offsetX, 274 - offsetY);
//Values may not be accurate. You can set yourself with shape tool.
CGPathCloseSubpath(path);
[shapeMask setPath:path];
[blueShape addChild:shapeMask];
}
关于ios - SKSpriteNode 来自图像触摸检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26738614/