views:

302

answers:

2

Hello! I have a CCLabelAtlas that I have on a layer to display the current score within my game...the problem that I am having with it is that it does not update when I use [scoreLabel setString:]. Here's how I have it laid out:

In the header:

@interface GameScene : CCLayer {
...

CCLabelAtlas* scoreLabel;
}

And in the init:

scoreLabel = [[CCLabelAtlas alloc] initWithString:@"0" charMapFile:@"scoreCharMap.png" itemWidth:6 itemHeight:8 startCharMap:'.'];
[scoreLabel setPosition:ccp(105, 414)];
[self addChild:scoreLabel z: 6];

scoreCharMap.png is a custom map that includes ./0123456789 of my desired font. When the score is changed, I attempt to do this to get the label to update with the current score:

NSString* str = [[NSString alloc] initWithFormat:@"%d", [[GameRunner sharedInstance] currentScore]];
[scoreLabel setString: str];
[str release];
[scoreLabel draw];

Problem is that it doesn't ever update - it just sits there and displays 0. I know for a fact due to breakpoints and debugging that setString is being called, and that the string that it should be displaying is correct - but it just doesn't update. Hard-coding the value and saying [scoreLabel setString:@"1234"] does nothing, either. What am I doing wrong here? I am using Cocos2d 0.99 - thanks in advance!

A: 

Maybe this is something wrong with the font you are using? Try using one of the font maps that came with Cocos2D and see if it works for you.

Rob Segal
Even when using the fps_images.png that Cocos2d uses for the frame rate LabelAtlas I have no luck.
JonnyFunFun
Didn't thank that would work but it was a random thought. Can you provide a more comprehensive code samples like the entire GameScene class and the entire file where you are updating the score?Also have you tried posting your question in the Cocos2D forums...http://www.cocos2d-iphone.org/forum/
Rob Segal
A: 

The method -[CCLabelAtlas setString:] does a few things.

Can you verify that following goes right: (place a breakpoint and step through the function)

  • resizeCapacity doesn't fail to resize
  • updateAtlasvalues retreives a UTF8 character array pointer. Inspect that to see if that's the correct string, and while you're there, inspect n in the same method, which should be your string length

See code for setString below:

- (void) setString:(NSString*) newString {
    if( newString.length > textureAtlas_.totalQuads )
        [textureAtlas_ resizeCapacity: newString.length];

    [string_ release];
    string_ = [newString retain];
    [self updateAtlasValues];

    CGSize s;
    s.width = [string_ length] * itemWidth;
    s.height = itemHeight;
    [self setContentSize:s];
}

Let me know what the results are, if you still need any help.

nash