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そして、が初期化された後、別のスプライトがその場所で初期化され、userDatanilのままになったのも不思議ではありません!

+(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 に答える