添加分数标签Cocos2d 3.0

时间:2014-02-12 01:01:17

标签: ios cocos2d-iphone label

我得分为int:

int score=0;

我理解如何添加到分数中,我只是不知道如何显示它

这就是我所拥有的,但我很确定它错了:

CCLabelTTF *scorelabel = [CCLabelTTF labelWithString:@"score" fontName:@"Verdana-Bold" fontSize:18.0f];

int score=0;
CCLabelTTF *scorelabel = [CCLabelTTF labelWithString:@"score" fontName:@"Verdana-Bold" fontSize:18.0f];
[self addChild:scorelabel];

有关如何在场景中显示分数的建议吗?

谢谢

现在代码

int score=0;
CCLabelTTF *scorelabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"score: %d",score] fontName:@"Verdana-Bold" fontSize:18.0f];  **The warning**
scorelabel.positionType = CCPositionTypeNormalized;
scorelabel.position = ccp(0.0f, 0.0f);
[self addChild:scorelabel];

返回按钮

CCButton *backButton = [CCButton buttonWithTitle:@"[ Menu ]" fontName:@"Verdana-Bold" fontSize:18.0f];
backButton.positionType = CCPositionTypeNormalized;
backButton.position = ccp(0.85f, 0.95f); // Top Right of screen
[backButton setTarget:self selector:@selector(onBackClicked:)];
[self addChild:backButton];

2 个答案:

答案 0 :(得分:1)

您需要将标签添加到场景中。如果您在场景的init方法中,则可以使用以下行:

[self addChild:scorelabel];

此外,您的分数标签包含文本“分数”,但不包括实际分数。如果要包含分数,请将标签的创建更改为:

CCLabelTTF *scorelabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"score: %d",score] fontName:@"Verdana-Bold" fontSize:18.0f];

答案 1 :(得分:0)

@connor建议的最后一个解决方案是正确的。我怀疑你可能遇到了重叠层的问题(我也面临同样的问题并且理解它常常难以弄清楚)。

@implementation HelloWorldScene {
  //...
  CCLabelTTF * _label;
}

- (id)init
{
  self = [super init];
  //...
  _button     = [CCSprite spriteWithImageNamed:@"button@2x.png"];
  _label      = [CCLabelTTF labelWithString:@"A Button" fontName:@"Helvetica Neue" fontSize:14.0f];
  _button.position      = ccp( 100.0f, 100.0f);
  _label.position       = ccp( 100.0f, 100.0f);

  [self addChild:_button    ]; //make sure a button or any other layer goes before the label
  [self addChild:_label     ]; //label or any kind of text must be added last
}