Set touchable area of SKSpriteNode

最后都变了- 提交于 2019-12-11 04:57:52

问题


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

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