2013-11-24 35 views
3

我正在嘗試修改已經添加到SKNode的SKShapeNode。更新附加到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

嘗試刪除所有兒童和重新進行添加新的子

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

這看起來像一個答案。也許有一種方法可以更新孩子的財產? – user2331875

2

SKShapeNodeSKScene的屬性:

@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]; 
}