2014-10-30 92 views
1

是否有SKSpriteNode的属性可让您手动设置其可触摸区域?设置SKSpriteNode的可触摸区域

我有一个PNG纹理的精灵,它似乎只能检测到PNG的不透明部分的触摸。所以大空白画布内的一个小圆圈实际上有一个很小的可触摸区域。

回答

1

创建一个您希望可触摸区域大小的SKNode。添加纹理精灵作为新SKNode的子节点。检查是否触摸了新的SKNode,而不是触摸纹理的精灵。

+0

我在设置SKNode的帧大小时遇到​​问题 - 当我修改该属性时,它内部的精灵将消失。但这似乎是正确的路要走。 – 2014-10-30 19:36:46

+0

在添加纹理spriteNode之前设置SKNode及其其他属性的大小 – meisenman 2014-10-30 19:51:06

0
-(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]; 
}