使用枚举更改精灵的颜色

时间:2016-03-03 18:05:15

标签: ios sprite-kit

你好一点点都坚持这个, 我希望通过名字枚举它们来添加到屏幕上的精灵(我在枚举中的第一次尝试),所以我很确定它会是一些我错过的简单 - 代码如下 任何帮助表示感谢。

将精灵添加到图层:

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;

}

在测试时无法找到精灵,所以我必须定位错误的对象&gt;&lt;还是什么?

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

        }}

1 个答案:

答案 0 :(得分:1)

您的精灵位于名为SKNode的{​​{1}}。

然后MyLabel被添加到场景中。

您现在正在实际场景中调用MyLabel

不幸的是,这个函数不被称为enumerateDecendentNodesWithName,并且不会生成孙子或曾孙子节点。

相反,您需要枚举您需要查找的节点的父节点,在本例中为enumerateChildNodesWithName

MyLabel