从for循环中获取SKNode数组中的userData

时间:2014-05-09 11:03:19

标签: objective-c sknode

从我的对象获取userData值时遇到一些问题。我知道正在为对象设置userData,因为在我的代码中我可以毫无问题地访问它。

我创建了一个SKNode * column3来保存该列中的所有按钮。这被添加到场景中:

    column3 = [SKNode node];
    [self addChild:column3];
    column3.name = @"column3";

当我创建按钮时,我将它们分配到适当的列,如下所示:

    [column3 addChild:newButton];

稍后在我的代码中,我需要遍历column3节点组并从该组中的每个对象获取@“buttonRow”userData。出于某种原因,它只给了我“NULL”的值。 NSLog就是我用来测试的,它们对我来说并不重要。

我需要获取此userData,以便在从该列删除/删除任何按钮时,将所有按钮向下移动以占用屏幕上的任何空白区域。游戏将向下移动按钮并将新的按钮添加到顶部以填充列备份。

我尝试将column3.children更改为self.children,它将我所有列节点IE / column1,column2,column3等游戏给我。所以我不确定为什么它不起作用。正在阅读并试图找出一段时间。

 for(SKNode * child in column3.children) { //loop through all children in Column3.

                SKSpriteNode* sprite = (SKSpriteNode*)child;
                NSString* name = sprite.name;
                NSLog(@"child name %@", name);

                NSString *childRowTmp = [child.userData objectForKey:@"buttonRow"];
                NSLog(@"child row %@", childRowTmp);

                int childRowNumber = [childRowTmp intValue];
                NSLog(@"child row # %i", childRowNumber);
            }

任何帮助或提示都会很棒,谢谢。

更新 这是我使用我创建的按钮类文件创建按钮的方法。

    //Create Blue Button.
    NSString *rand = [self genRandStringLength:10];
    newButton = [[ButtonNode alloc] initWithButtonType:1 column:3 row:i uniqueID:rand];
    newButton.name = rand;

    [column3 addChild:newButton];   //add the button to correct column
    [column3Array addObject:newButton];

    blueTotal++;
    totalButtons++;
    column3Total++;

这是创建Object的自定义类文件。

-(id)initWithButtonType:(int)buttonType column:(int)buttonColumn row:(int)buttonRow uniqueID:(NSString *)uniqueID {
    self = [super init];

    uniqueStr = uniqueID;

    rowNumber = buttonRow;

    columnNumber = 3;   //this is just hard coded for testing

     buttonNumber = 1;
     [self addButtonBlue];
}

这是创建和添加按钮

的类的一部分
- (void) addButtonBlue {

    SKSpriteNode *button;

        //button type 1
        button = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:kBoxSize];

        button.name = uniqueStr;

        button.physicsBody.categoryBitMask = blueCategory;
        button.physicsBody.contactTestBitMask = blueCategory;
        button.physicsBody.collisionBitMask = blueCategory | redCategory | yellowCategory | greenCategory | orangeCategory;

        NSString *tmpColumn = [NSString stringWithFormat:@"%i",columnNumber];
        NSString *tmpType = [NSString stringWithFormat:@"%i",buttonNumber];
        NSString *tmpRow = [NSString stringWithFormat:@"%i",rowNumber];

        button.userData = [NSMutableDictionary dictionary];
        [button.userData setValue:uniqueStr forKey:@"buttonID"];
        [button.userData setValue:tmpType forKeyPath:@"buttonType"];
        [button.userData setValue:tmpColumn forKeyPath:@"buttonColumn"];
        [button.userData setValue:tmpRow forKey:@"buttonRow"];

        button.position = CGPointMake(xPos , yPos );

        [self addChild:button];
}

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,请参阅下面生成绿色或红色精灵的代码。试图在SKSpriteNode.userData范围内给出颜色值。在我的日志输出中,userData(null)

+(SKSpriteNode *)randomSprite {
    SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"green.png"];
    [sprite setUserData:[NSMutableDictionary dictionary]];

    if ((arc4random() %(2)-1) == 0){
        sprite = [SKSpriteNode spriteNodeWithImageNamed:@"green.png"];
        [sprite.userData setValue:@"GREEN" forKey:@"COLOR"];
    }else {
        sprite = [SKSpriteNode spriteNodeWithImageNamed:@"red.png"];
        [sprite.userData setValue:@"RED" forKey:@"COLOR"];
    }

    NSLog(@"user data = %@",sprite.userData);

    sprite.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:sprite.size.width/2];
    sprite.physicsBody.dynamic = YES;

    return sprite;
}

对于解决方法,我想我们可以创建一个参考字典,其中SKSpriteNode实例是键,NSDictionary数据是值。

// ----- * 更新** -------

并且毫无疑问,在初始化userData之后,另一个精灵正在其位置初始化,留下userData零!

+(SKSpriteNode *)randomSprite {
    SKSpriteNode *sprite;

    if ((arc4random() %(2)-1) == 0){
        sprite = [SKSpriteNode spriteNodeWithImageNamed:@"green.png"];
        [sprite setUserData:[NSMutableDictionary dictionary]];
        [sprite.userData setValue:@"GREEN" forKey:@"COLOR"];
    }else {
        sprite = [SKSpriteNode spriteNodeWithImageNamed:@"red.png"];
        [sprite setUserData:[NSMutableDictionary dictionary]];
        [sprite.userData setValue:@"RED" forKey:@"COLOR"];
    }

    NSLog(@"user data = %@",sprite.userData);

    sprite.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:sprite.size.width/2];
    sprite.physicsBody.dynamic = YES;

    return sprite;
}