1

我在从我的对象中获取 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++;

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

-(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];
}
4

1 回答 1

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;
}
于 2014-07-02T12:50:57.820 回答