【问题标题】:Displaying score overlapping显示分数重叠
【发布时间】:2013-05-06 09:19:48
【问题描述】:

我使用了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];

【问题讨论】:

    标签: iphone cocos2d-iphone uilabel cocos2d-x


    【解决方案1】:

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

    在.h中

    CCLabelTTF *label1;
    

    在.m

    -(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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 1970-01-01
      • 2016-06-05
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多