Dynamic 保存在.plist文件中的六边形数和六边形位置。如何检索第一项并将其分配给CCSprite?

Dynamic 保存在.plist文件中的六边形数和六边形位置。如何检索第一项并将其分配给CCSprite?,dynamic,cocos2d-iphone,naming,Dynamic,Cocos2d Iphone,Naming,这是plist文件,其中包含六边形位置的字典。六边形的位置是一个数组,其中存储了另一个字典,其中存储了六边形位置的x值和y值 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <d

这是plist文件,其中包含六边形位置的字典。六边形的位置是一个数组,其中存储了另一个字典,其中存储了六边形位置的x值和y值

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>level1</key>
    <dict>
        <key>hexposition</key>
        <array>
            <dict>
                <key>xVal</key>
                <integer>105</integer>
                <key>yVal</key>
                <integer>160</integer>
            </dict>
            <dict>
                <key>xVal</key>
                <real>172.5</real>
                <key>yVal</key>
                <real>199.5</real>
            </dict>
            <dict>
                <key>xVal</key>
                <integer>240</integer>
                <key>yVal</key>
                <integer>238</integer>
            </dict>
            <dict>
                <key>xVal</key>
                <real>307.5</real>
                <key>yVal</key>
                <real>199.5</real>
            </dict>
            <dict>
                <key>xVal</key>
                <integer>375</integer>
                <key>yVal</key>
                <integer>160</integer>
            </dict>
            <dict>
                <key>xVal</key>
                <real>307.5</real>
                <key>yVal</key>
                <real>120.5</real>
            </dict>
            <dict>
                <key>xVal</key>
                <integer>240</integer>
                <key>yVal</key>
                <integer>82</integer>
            </dict>
            <dict>
                <key>xVal</key>
                <real>172.5</real>
                <key>yVal</key>
                <real>120.5</real>
            </dict>
            <dict>
                <key>xVal</key>
                <integer>240</integer>
                <key>yVal</key>
                <integer>160</integer>
            </dict>
        </array>
    </dict>
如何使数组的第一项成为名为hexagon1的CCSprite?那么下一个名字是hexagon2?是否可以动态更改名称

-(void) generateLevelFromPlist:(int)currentLevel{

        NSString *mainPath = [[NSBundle mainBundle] bundlePath];
        itemPositionPlistLocation = [mainPath stringByAppendingPathComponent:@"levelconfig.plist"];
        NSDictionary * itemPositions = [[NSDictionary alloc] initWithContentsOfFile:itemPositionPlistLocation];
        NSString *myString = [NSString stringWithFormat:@"level%d", currentLevel];
        NSLog(@"This is the string %@", myString);

        int hexCount = [[[itemPositions valueForKey:myString]valueForKey:@"hexposition"] count];

        for (int i=0;i<hexCount;i++){
            NSMutableArray *whichHexagon = [[NSMutableArray alloc]initWithArray:[[itemPositions valueForKey: myString ]valueForKey:@"hexposition"]];
            NSNumber *generatedXVal = [[[[itemPositions valueForKey: myString ]valueForKey:@"hexposition"] objectAtIndex:i]valueForKey: @"xVal"];
            int xVal = [generatedXVal integerValue];
            NSNumber *generatedYVal = [[[[itemPositions valueForKey: myString ]valueForKey:@"hexposition"] objectAtIndex:i]valueForKey: @"yVal"];
            int yVal = [generatedYVal integerValue];
            NSLog(@"the hexagon would have the position %d, %d", xVal,yVal);

           // Here I want to create hexagons with numbers that are ascending (hexagon1, hexagon2,hexagon3,...) [DYNAMICALLY, if possible]
            // Then I want to make a CCSprite out of it, in order to use it globally 

        }

如果您正在谈论在运行时创建动态命名变量hexagon1、hexagon2等,我认为这是不可能的。但是,您可以将这些名称生成为字符串,并将它们用作包含精灵的字典的键。比如:

hexagonSprites_ = [[NSMutableDictionary alloc] init];
NSArray *hexPositions = [[itemPositions valueForKey:myString] valueForKey:@"hexposition"];
int hexCount = [hexPositions count];

for (int i = 0; i < hexCount; i++) {
    NSString *key = [NSString stringWithFormat:@"hexagon%d",i];

    CCSprite *sprite = .... // generate sprite here

    [hexagonSprites_ setObject:sprite forKey:key];
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>level1</key>
    <dict>
        <key>hexposition</key>
        <array>
            <string>{105,160}</string>
            <string>{172.5,199.5}</string>
            <string>{240,238}</string>
            <string>{307.5,199.5}</string>
        </array>
    </dict>
</dict>
</plist>
然后使用CGPointFromStringNSString*string函数将其转换为CGPoint:

CGPoint location = CGPointFromString([hexPositions objectAtIndex:i]);

如果您想在运行时创建名为hexagon1的CCSprite变量,可以在Objective-C运行时创建。但只需使用hexagon1键将CCSprite添加到字典中就容易多了。嘿,我想要android