2016-03-03 79 views
1

大家好一點卡在這一個, 我希望通過名稱列舉通過它們添加到屏幕上的精靈(我第一次嘗試在枚舉),所以我很確定它將是簡單的,我錯過了 - 代碼低於 任何幫助表示讚賞。使用枚舉更改精靈的顏色

添加精靈層:

for(int keyCount = 0; keyCount < ammountOfSprites; ++keyCount) 
    //other code here.... 

    Star01 = [SKSpriteNode spriteNodeWithImageNamed:@"Actions-rating-icon.png"]; 
    Star01.position = CGPointMake(-30,StarHeight); 
    [MyLabel addChild:Star01]; 
    **Star01.name** = [NSString stringWithFormat:@"Star01_%@",[NSString stringWithFormat:@"%@",dataFromArray]]; 
    NSLog(@"%@", Star01.name); 

///名稱登錄爲:

Star01_Owl 
Star02_Owl 
Star03_Owl 

觸摸內部開始

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
for (UITouch *touch in touches) { 
    CGPoint touchLocation = [touch locationInNode:self]; 
    SKNode *node = [self nodeAtPoint:touchLocation]; 

//colour to add to the 
UIColor *blackColour = [UIColor greenColor]; 


    //function takes sprite.names and enumerates them 
    //name match those logged however dont work. 
[self changeStarScorestar01:@"Star01_Owl" star02:@"Star02_Owl" star03:@"Star03_Owl" colour:blackColour]; 

後來......

//function takes sprite.names and enumerates them 
-(void)changeStarScorestar01:(NSString*)star01 star02:(NSString*)star02 star03:(NSString*)star03 colour:(UIColor*)Starcolour 
{ 

[self enumerateChildNodesWithName:star01 usingBlock:^(SKNode *node, BOOL *stop) 
    { 

     NSLog(@"node.name isEqualToString:star01"); 
     SKSpriteNode *star01Found = (SKSpriteNode*)[self childNodeWithName:star01]; 
    star01Found.color =Starcolour; 
    star01Found.colorBlendFactor =1.0; 

} 

關於測試它無法找到精靈,所以我必須針對錯誤的對象> <什麼?

[self enumerateChildNodesWithName:star01 usingBlock:^(SKNode *node, BOOL *stop) 
    { 
     if ([node.name isEqualToString:star01]) 
     { 
      NSLog(@"FOUND STAR"); 

     }} 
+1

因此,你有一個名爲'MyLabel'的SKNode中的小精靈?是否將MyLabel添加到場景中?如果是這樣,那麼我相信你需要枚舉'MyLabel'的孩子,而不是現場 – Knight0fDragon

+0

Knight0fDragon - 這的確是問題所在 - 謝謝:) – StackBuddy

回答

1

你有你的精靈在SKNodeMyLabel

然後MyLabel被添加到場景中。

您現在在實際的場景中調用enumerateChildNodesWithName

不幸的是,這個函數不叫做enumerateDecendentNodesWithName,並且不會影響到孫輩或孫子女的節點。

相反,您需要枚舉節點的父節點,您需要查找的節點的父節點,在本例中爲MyLabel

[MyLabelenumerateChildNodesWithName:star01 usingBlock:^(SKNode *node, BOOL *stop) 
{ 
    if ([node.name isEqualToString:star01]) 
    { 
     NSLog(@"FOUND STAR"); 
    } 
}]; 
+0

完成enumeratingSelf ThroughDecendentStagesOfInsanity :) 經驗教訓! 感謝Knight0fDragon! – StackBuddy

+0

如果你真的想要它,你總是可以編寫enumerateDecendentNodesWithName擴展函數,但我不會推薦它。哈哈。 – Knight0fDragon