2014-02-24 62 views
0

我想知道是否有一種簡單的方法,我可以帶SKNode並增加它被按下的區域。增加SKNode可按的區域

例如,目前我正在檢查,如果一個節點被點擊,像這樣:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint positionInScene = [touch locationInNode:self]; 
    SKNode *node = [self nodeAtPoint:positionInScene]; 
    if ([node.name isEqualToString:TARGET_NAME]) { 
     // do whatever 
    } 
    } 
} 

如果屏幕上繪製的節點是像40個像素×40個像素,是有辦法,如果用戶點擊節點的10個像素內,它會呈現爲點擊?

謝謝!

+2

難道你不能只讓節點50 x 50,只需填寫(或顏色或繪製)中間的40 x 40? –

回答

2

您可以將一個不可見的精靈節點作爲子節點添加到可見節點。讓子節點的大小大於可見節點的大小。

例如,在OSX這將在現場工作:

-(id)initWithSize:(CGSize)size {  
    if (self = [super initWithSize:size]) { 
     SKSpriteNode *visibleNode = [[SKSpriteNode alloc] initWithColor:[NSColor yellowColor] size:CGSizeMake(100, 100)]; 
     visibleNode.name = @"visible node"; 
     visibleNode.position = CGPointMake(320, 240); 

     SKSpriteNode *clickableNode = [[SKSpriteNode alloc] init]; 
     clickableNode.size = CGSizeMake(200, 200); 
     clickableNode.name = @"clickable node"; 

     [visibleNode addChild:clickableNode]; 
     [self addChild:visibleNode]; 
    } 
    return self; 
} 

-(void)mouseDown:(NSEvent *)theEvent 
{ 
    CGPoint positionInScene = [theEvent locationInNode:self]; 
    SKNode *node = [self nodeAtPoint:positionInScene]; 
    NSLog(@"Clicked node: %@", node.name); 
} 

可點擊節點從可見節點的邊緣向外延伸50像素。點擊此處將輸出「Clicked node:clickable node」。

因爲可點擊節點覆蓋它,所以名爲「visible node」的節點永遠不會被調用返回[self nodeAtPoint:positionInScene]。

相同的原則適用於iOS。