更新附加到SKNode的SKShapeNode

时间:2013-11-24 19:35:10

标签: objective-c sprite-kit

我正在尝试修改已添加到SKNode的SKShapeNode。

这是我将SKNode添加到屏幕并将SKShapeNode附加到其中的代码。现在我试图修改特定SKShapeNode的颜色,但我不知道该怎么做。有什么建议吗?

SKNode *dot = [SKNode node];

SKShapeNode *circle = [SKShapeNode node];
circle.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 20, 20)].CGPath;
circle.fillColor = [UIColor blueColor];
circle.strokeColor = [UIColor blueColor];
circle.glowWidth = 5;
[dot addChild:circle];

[self addChild:dot];

2 个答案:

答案 0 :(得分:2)

尝试删除所有孩子并阅读新孩子

[dot removeAllChildren];
[dot  addChild:circle];

答案 1 :(得分:2)

SKShapeNode的{​​{1}}属性设为SKScene

@interface YourScene()
@property SKShapeNode *circle;
@end

将创建圆圈的代码更改为:

self.circle = [SKShapeNode node];
self.circle.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 20, 20)].CGPath;
self.circle.fillColor = [UIColor blueColor];
self.circle.strokeColor = [UIColor blueColor];
self.circle.glowWidth = 5;
[dot addChild:self.circle];

现在,您可以在场景中的任何位置访问circle节点:

- (void)changeColor {
    self.circle.fillColor = [SKColor redColor];
}

另一个选择是为节点命名:

SKShapeNode *circle = [SKShapeNode node];
.....
circle = @"circle";

按名称访问该节点

- (void)changeColor {
    // Assuming the dot node is a child node of the scene
    SKShapeNode *circle = (SKShapeNode*)[self.scene childNodeWithName:@"/circle"];
    circle.fillColor = [SKColor redColor];
}