The number of hexagons and the position of the hexagons saved in the .plist file. How can I get the first item and assign it to CCSprite?
It is a dictionary plist file that contains the position of the hexagons. The position of the hexagons is an array that stores another dictionary for the x and y values for the position of the hexagon.
<?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 do I make the first element of the CCSprite array named "hexagon1"? Then the next one named "hexagon2"? Can I 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
}
source to share
If you are talking about creating dynamically named variables hexagon1
, hexagon2
etc. at runtime, I don't think it's possible. However, you can generate these names as strings and use them as keys to the 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, instead of storing the coordinates in dictionary form, you can store it as a string with {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 the function CGPointFromString(NSString *string)
to convert it to CGPoint:
CGPoint location = CGPointFromString([hexPositions objectAtIndex:i]);
source to share