如何在Cocos2d中更新和显示分数整数?

Dun*_*can 8 iphone xcode cocos2d-iphone

我显然正在制作一个有分数的游戏.如何调用更新方法并在右上角实际显示整数?

tal*_*n11 8

在这里,这可能会奏效

在.h文件中:

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

- (void)addPoint;
Run Code Online (Sandbox Code Playgroud)

在.m文件中:

在init方法中:

//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];
Run Code Online (Sandbox Code Playgroud)

别的地方:

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

现在只需致电:[self addPoint]; 每当用户杀死敌人时.

这应该工作,告诉我,如果没有,因为我没有测试它.