How to have a Score Integer updated and displayed in Cocos2d?

10,139

Solution 1

Here, this might work

In the .h file:

@interface HelloWorld : CCLayer {
    int score;    
    CCLabelTTF *scoreLabel;
}

- (void)addPoint;

In the .m file:

In the init method:

//Set the score to zero.
score = 0;

//Create and add the score label as a child.
scoreLabel = [CCLabelTTF labelWithString:@"8" fontName:@"Marker Felt" fontSize:24];
scoreLabel.position = ccp(240, 160); //Middle of the screen...
[self addChild:scoreLabel z:1];

Somewhere else:

- (void)addPoint
{
    score = score + 1; //I think: score++; will also work.
    [scoreLabel setString:[NSString stringWithFormat:@"%@", score]];
}

Now just call: [self addPoint]; whenever the user kills an enemy.

That should work, tell me if it didn't because I have not tested it.

Solution 2

in header file:

@interface GameLayer : CCLayer
{
    CCLabelTTF *_scoreLabel;
}

-(void) updateScore:(int) newScore;

in implementation file:

-(id) init
{
    if( (self=[super init])) {
      // ..

      // add score label
      _scoreLabel = [CCLabelTTF labelWithString:@"0" dimensions:CGSizeMake(200,30) alignment:UITextAlignmentRight fontName:@"Marker Felt" fontSize:30]; 
      [self addChild:_scoreLabel];
      _scoreLabel.position = ccp( screenSize.width-100, screenSize.height-20);

    }
    return self;
}

-(void) updateScore:(int) newScore {
    [_scoreLabel setString: [NSString stringWithFormat:@"%d", newScore]];
}

EDIT: if you don't want to use an ivar, you can use tags:

[self addChild:scoreLabel z:0 tag:kScoreLabel];
// ...
CCLabelTTF *scoreLabel = (CCLabelTTF*)[self getChildByTag:kScoreLabel];

EDIT 2: For performance reasons you should switch to CCLabelAtlas or CCBitmapFontAtlas if you update the score very frequently.

Also read the cocos2d programming guide about labels.

Share:
10,139

Related videos on Youtube

skippy_winks
Author by

skippy_winks

Updated on May 28, 2022

Comments

  • skippy_winks
    skippy_winks almost 2 years

    I am obviously making a game that has a score. How do I call an update method and have the integer actually displayed in the Top-Right corner?

  • JustSid
    JustSid about 13 years
    @ipodfreak0313: No need for CAPS, UILabel works fine for Cocos2D too, I mean, you render the content into a UIView too and can add other stuff into the view hierarchy. Plus: UIKits text rendering is far better than Cocos2Ds.
  • tallen11
    tallen11 about 13 years
    @JustSid: I'm pretty sure that it is recommended not to render UIKit elements on top of a cocos2d scene, but I may be wrong.
  • JustSid
    JustSid about 13 years
    @allthewayapps: There are some general tips/guidelines on how to handle UIViews on top of an EAGLLayer view, but Apple made its very own techdemo on how to render fast and how to render UI in a 3D OpenGL ES game and they also used UIKit (see the WWDC 2010 videos for more info).
  • skippy_winks
    skippy_winks about 13 years
    do I have to schedule an update that calls the function updateScore:(int)newScore ?
  • skippy_winks
    skippy_winks about 13 years
    thanks again, ive noticed you appearing in a few of my questions! and thanks for your help. now check out my most recent question about an AI. I could REALLY use some help there.
  • tallen11
    tallen11 about 13 years
    @ipodfreak0313: Haha. Glad that I could help! =) I took a look at your AI question, but unfortunately I have never tried to make my own AI before. I might have some ideas though. I'll think about it and let you know if I come up with anything.
  • skippy_winks
    skippy_winks about 13 years
    thanks man, i wish i could answer some questions for you, but i am still very new to cocos2d (okay, maybe not new, but i have a lot to learn).