9

I am looking to persistently display a game score in an iPhone app using cocos2d. Going off the code that cocos2d shows the FPS the app is running at:

-(void) showFPS
{
    frames++;
    accumDt += dt;

    if ( accumDt > 0.1)  {
        frameRate = frames/accumDt;
        frames = 0;
        accumDt = 0;
    }

    NSString *str = [NSString stringWithFormat:@"%.1f",frameRate];
    [FPSLabel setString:str];
    [FPSLabel draw];
}

I can get the score to display properly, but it flickers, even though the app is running at faster that 60 FPS... Any ideas?

afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
user21293
  • 6,439
  • 11
  • 44
  • 57
  • One thing about this code that you showed, don't message FPSLabel with draw. Instead, use [FPSLabel setNeedsDisplay:YES] after you update it. This should cancel your flicker. – Jason Coco Dec 03 '08 at 16:38

2 Answers2

9

For anyone who might be interested, I ended up using a cocos2d Label as so:

scoreLabel = [Label labelWithString: [NSString stringWithFormat:@"%d", score] dimensions: CGSizeMake(180, 20) alignment: UITextAlignmentRight fontName:@"Arial" fontSize: 20];
[scoreLabel setPosition: cpv(100,100)];
[self add: scoreLabel];

Hopefully this can help someone else.

Macarse
  • 91,829
  • 44
  • 175
  • 230
user21293
  • 6,439
  • 11
  • 44
  • 57
  • 2
    As of Cocos2d 0.8, you should use a BitmapFontAtlas, as it is way faster for something that's updated frequently on screen. – Brad Parks Oct 16 '09 at 18:58
5

Try using LabelAtlas instead. It is faster (it consumes much less CPU). See the AtlasDemo that comes with the cocos2d distribution to see how to use it.