views:

761

answers:

1

I am trying to create a cool score counter in my iPhone game, where I created the digits 0 to 9 in photoshop and I want to update the score every second.

What I am doing now is the following:

  • In my init I load all the digit sprites into an array, so that the array has 10 items.

  • I created a method which breaks down the current score (eg. 2000) into single digits and gets the sprites from the array and after that adds them to a parent CocosNode* object.

  • Every second I get the parent CocosNode by it's tag and replace it with the new parent object.

Currently I am already running into problems with this, because the score 2000 uses the 0-digit three times and I cannot re-use sprites.

- (CocosNode*) createScoreString:(int) score
{
    NSLog(@"Creating score string : %d", score);
    NSString* scoreString = [NSString stringWithFormat:@"%d", score];

    int xAxes = 0;
    CocosNode* parentNode = [[Sprite alloc] init];
    for (NSInteger index = 0; index < [scoreString length]; index++)
    {
     NSRange range;
     range.length = 1;
     range.location = index;

     NSString* digit = [scoreString substringWithRange:range];

     Sprite* digitSpriteOriginal = [self.digitArray objectAtIndex:[digit intValue]];
     Sprite* digitSprite = [digitSpriteOriginal copy];
     [digitSprite setPosition:cpv(xAxes, 0)];

     xAxes += [digitSprite contentSize].width - 10;

     [parentNode addChild:digitSprite];
    }
    return parentNode;
}

Am I handling this the right way within cocos2d or is there some standard functionality for this? Also, if this is correct, how can I 'reuse' the sprites?

+2  A: 

I believe that you want to use the LabelAtlas class, you'll only need to provide a compatible bitmap (like one the fps counter uses).

arul
Thanks a lot! After some searching I came across this solution. I even modified the LabelAtlas class to support padding of characters. I will provide a patch later.
Wim Haanstra