我想知道是否有一种简单的方法可以使用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个像素内点击,是否会将其渲染为点击?
谢谢!
答案 0 :(得分: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);
}
可点击节点从可见节点的边缘向外延伸50px。单击此内容将输出“Clicked node:clickable node”。
对[self nodeAtPoint:positionInScene]的调用永远不会返回名为“visible node”的节点,因为可点击节点会覆盖它。
同样的原则适用于iOS。