0

我使用了来自http://www.raywenderlich.com/25736/how-to-make-a-simple-iphone-game-with-cocos2d-2-x-tutorial的cocos2d 游戏教程将分数添加到 scorelabel 后,分数增加,但之前的分数没有被删除,新分数被添加到之前分数的标签之上

代码:

CGSize winSize = [[CCDirector sharedDirector] winSize];
CCLabelTTF * label1 = [CCLabelTTF labelWithString:@"_monsterdestroyed" fontName:@"Arial" fontSize:32];
score=score + 2;

[label1 setString:[NSString stringWithFormat:@"%d",score]];

label1.color = ccc3(0,0,0);
label1.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:label1];
4

1 回答 1

0

我猜你是在一个更新方法中做这一切,在每次更新时添加一个 label1 标签。您可能希望在您的 .h 文件中为分数 label1 设置一个 iVar,在您的 init 中对其进行初始化,如下所示:

在.h

CCLabelTTF *label1;

-(id) init {
    if (self = [super init]) {
        // your existing code, add the following
        CGSize winSize = [[CCDirector sharedDirector] winSize];

        label1 = [CCLabelTTF labelWithString:@"0" fontName:@"Arial" fontSize:32];
        label1.color = ccc3(0,0,0);
        label1.position = ccp(winSize.width/2, winSize.height/2);
        [self addChild:label1];  
   }     
 }


 // where you update the score
 score = score + 2;
 [label1 setSting:[NSString stringWithFormat:"%i", score];
于 2013-05-06T10:23:12.577 回答