[英]Adding Score Label Cocos2d 3.0
我的得分是整数:
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];
您需要将标签添加到场景中。 如果您处于场景的init方法中,则可以使用以下行:
[self addChild:scorelabel];
另外,您的分数标签包含文本“分数”,但不包括实际分数。 如果要包括分数,请将标签的创建更改为此:
CCLabelTTF *scorelabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"score: %d",score] fontName:@"Verdana-Bold" fontSize:18.0f];
@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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.