1

I have got a for loop where 9 hexagons (hexagon1 through hexagon9) have to be created... But I cannot use hexString as the name of the Sprite because it is a NSString, right ? So how would I make it right ?

hexString [<- I want the for loop to generate "hexagon1", then "hexagon2" and so on instead of the NSString] = [self createHexagon:ccp(xVal,yVal) :i];

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

    for (int i=1;i<=hexCount;i++){
        NSString *hexString = [NSString stringWithFormat:@"hexagon%d",i];
        NSNumber *generatedXVal = [[[[itemPositions valueForKey: myString ]valueForKey:@"hexposition"] valueForKey: hexString]valueForKey: @"xVal"];
        int xVal = [generatedXVal integerValue];
        NSNumber *generatedYVal = [[[[itemPositions valueForKey: myString ]valueForKey:@"hexposition"] valueForKey: hexString ]valueForKey: @"yVal"];
        int yVal = [generatedYVal integerValue];

        hexString = [self createHexagon:ccp(xVal,yVal) : i];
        NSLog(@"%@", hexString);
    }
the_critic
  • 12,720
  • 19
  • 67
  • 115
  • possible duplicate of [variable name from string in obj-c](http://stackoverflow.com/questions/3164860/variable-name-from-string-in-obj-c) – jscs Dec 04 '11 at 19:48
  • See also: http://stackoverflow.com/questions/8090590/is-this-possible-to-call-variable-dynamically-in-objective-c and all the questions linked in the comment there. – jscs Dec 04 '11 at 19:49
  • Almost exact duplicate: http://stackoverflow.com/questions/8376824/number-of-hexagons-and-position-of-hexagons-saved-in-plist-file-how-can-i-retr – Lukman Dec 06 '11 at 07:27

3 Answers3

3

That would be impossible as you didn't declare the variable. A work around for this would be using an NSArray and saving your data on to the array instead of making a list of variables.

TheAmateurProgrammer
  • 9,252
  • 8
  • 52
  • 71
  • To save your hexagons. So you would save your hexagon one at the index 0 of the array and hexagon 2 at index 1 of the array and so on. – TheAmateurProgrammer Dec 04 '11 at 13:34
  • Ok, so if I store hexagon1 in the plist as a NSDictionary with the hexagon's x and y value in an array called hexposition, how can I assign hexagon1 or better said, the first item of the array to a CCSprite in order to use it throughout the game ? – the_critic Dec 04 '11 at 16:26
1

Use NSMutableDictionary.

for (int i=1;i<=hexCount;i++){
    NSString *hexString = [NSString stringWithFormat:@"hexagon%d",i];
    CCSprite *sprite = [self doSomethingToGetSprite];

    [mutableDictionary setObject:sprite forKey:hexString];
}        

Later you can iterate over all the sprites in the dictionary using:

for (NSString *key in mutableDictionary) {
    CCSprite *sprite = [mutableDictionary objectForKey:key];
    [self doStuffWithSprite:sprite];
}

By the way, why are you overwriting hexString that you assign here:

NSString *hexString = [NSString stringWithFormat:@"hexagon%d",i];

With the one here:

hexString = [self createHexagon:ccp(xVal,yVal) : i];

And that method call is an obvious syntax error with the dangling : i part there.

Lukman
  • 18,462
  • 6
  • 56
  • 66
  • Right, I know it was wrong overwriting the NSString, that is why I asked. I just wanted to note that there is the problem ... But thank you very much, your post helped me a lot ! – the_critic Dec 06 '11 at 12:41
0

can you use NSSelectorFromString?

RolandasR
  • 3,030
  • 2
  • 25
  • 26