Number of hexagons and position of hexagons saved in .plist file. How can I retrieve the first item and assign it to a CCSprite?

ぃ、小莉子 提交于 2019-12-11 15:37:11

问题


This is the plist file whith a dictionary that contains the position of the hexagons. The position of the hexagons is an array where another dictionary for x and y value for the position of the hexagon is stored.

<?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>

How would I make the first item of the array a CCSprite with the name "hexagon1" ? Then the next one with the name "hexagon2" ? Is it possible to change the name dynamically ?

-(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 

        }

回答1:


If you are talking about creating dynamically named variables hexagon1, hexagon2 etc during runtime, I don't think it's possible. However, you can generate those names as strings and use them as the keys to a dictionary that will contain your sprites. Something like:

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

Also, rather than storing the coordinates in the form of dictionary, you can store it in the form of string {x,y} something like:

<?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>

Then use CGPointFromString(NSString *string) function to convert it to CGPoint:

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


来源:https://stackoverflow.com/questions/8376824/number-of-hexagons-and-position-of-hexagons-saved-in-plist-file-how-can-i-retr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!