问题
Is there a property of SKSpriteNode
that lets you manually set its touchable area?
I have a sprite textured with a PNG, it only seems to detect touches on the opaque portion of the PNG. So a small circle inside a large blank canvas actually has a tiny touchable area.
回答1:
Create an SKNode that is the size you would like the touchable area to be. Add your textured sprite as a child of the new SKNode. Check to see if the new SKNode was touched instead of if the textured sprite was touched.
回答2:
-(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:@"childName"])
{
NSLog(@"You touched to child of sprite");
}
}
-(void)sprite
{
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"spriteImageName"];
[self addChild:sprite];
SKSpriteNode *spriteChild = [SKSpriteNode spriteNodeWithImageNamed:@"childImageName"];
spriteChild.name = @"childName";
[sprite addChild:spriteChild];
}
来源:https://stackoverflow.com/questions/26656896/set-touchable-area-of-skspritenode