Iphone 如何在cocos2d中显示游戏中的分数

Iphone 如何在cocos2d中显示游戏中的分数,iphone,cocos2d-iphone,Iphone,Cocos2d Iphone,我有一个问题,当我试图显示在cocos2d游戏的最后一层游戏的分数。有一种算法可以修改我的变量increment的值,该变量包含用户的点数,然后游戏会显示它们 increment = increment + 50; [pointLabel setString: [NSString stringWithFormat: @"Points: %i", increment]]; 然后一个函数控制用户是否在他的游戏中生活 -(void)gameOver:(int)value punteggio:(id)

我有一个问题,当我试图显示在cocos2d游戏的最后一层游戏的分数。有一种算法可以修改我的变量
increment
的值,该变量包含用户的点数,然后游戏会显示它们

increment = increment + 50;
[pointLabel setString: [NSString stringWithFormat: @"Points: %i", increment]];
然后一个函数控制用户是否在他的游戏中生活

-(void)gameOver:(int)value punteggio:(id)punti{

    if (value == 1) {
        //WIN
    }else if(value == 2){
        if (life > 1) { // 1
            life = life - 1;
            for (CCSprite *spr in spriteLifeArray) {
                if (life == spr.tag) {
                    [self removeChild:spr cleanup:YES];               
                }}} 
     else { 
            //  LOSE
            [...]
            [[CCDirector sharedDirector] replaceScene:[GameOver node]];
        }
}}
然后调用GameOverLayer。这是.h文件

@interface GameOver : CCNode { 
    CGSize size;
    CCLabelTTF *label1;
    CCLabelTTF *label2;
    CCLabelTTF *labelpnt;
    CCLabelTTF *labelscore;
}
-(void)restart;
这是.m文件

@implementation GameOver
+(id) scene {
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    GameOver *layer = [GameOver node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}


-(id) init{

    if( (self=[super init] )) {

        size = [[CCDirector sharedDirector] winSize];

        label1 = [CCLabelTTF labelWithString:@"Game Over" fontName:@"Marker Felt" fontSize:40];
        label1.position = ccp(size.width/2 , size.height/2+20+50 );

        labelpnt = [CCLabelTTF labelWithString:@"Punteggio" fontName:@"Marker Felt" fontSize:20];
        labelpnt.position = ccp(size.width/2 , size.height/2+50-100 );

        labelscore = [CCLabelTTF labelWithString:@"100" fontName:@"Marker Felt" fontSize:20];
        [labelscore setString: [NSString stringWithFormat: @" 0 "]];
        [labelscore setColor:ccc3(255, 1, 1)];
        labelscore.position = ccp(size.width / 2, size.height/2+50-130);


        label2 = [CCLabelTTF labelWithString:@"Ricomincia" fontName:@"Marker Felt" fontSize:25];
        CCMenuItemLabel *back = [CCMenuItemLabel itemWithLabel:label2  target:self selector:@selector(restart)];
        CCMenu *menu= [CCMenu menuWithItems:back, nil];
        menu.position = ccp(size.width/2 , size.height/2-50+50);

        [self addChild: label1];
        [self addChild: labelpnt];
        [self addChild: labelscore];
        [self addChild: menu];

    }
    return self;
}

-(void) restart {
    [[CCDirector sharedDirector] replaceScene:[HelloWorldLayer node]];
}

如何在游戏中显示我的
int增量的最终值?如何在类中传递它?

使用UserDefault。这是代码

    //Save score in game screen
    int highScore  = 234;
    [[NSUserDefaults standardUserDefaults] setInteger:12 forKey:@"HighScore"];
    [[NSUserDefaults standardUserDefaults] synchronize];


    //get score in game over
    int highScore  = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScore"];

    NSString *score = [NSString stringWithFormat: @"%d", highScore];
    CCLabelTTF *scoreLabel = [CCLabelTTF labelWithString:score fontName:@"Marker Felt" fontSize:40];

嘿,在这种情况下,你应该采取一个单独的层显示分数生活或任何你想同时显示

HUD层
平视显示
类中,您应该编写一些基本代码,在屏幕上显示一个
CCLabelBMFont
,屏幕上显示“您的分数”、“您赢了”或“您输了”,下面有一个按钮显示“重新启动”。 当点击重启按钮时,它会创建一个新的ActionLayer实例,并切换到该实例。 编写一个Score方法scoreUpdate,该方法将具有计算分数的逻辑,就像子弹击中怪物一样,并将其更新到
CCLabelBMFont
标签。 然后你需要做的就是调用这个方法

以下是针对此类需求的最佳教程之一


但这是单场比赛的分数还是所有比赛中最好的?使用此方法,我可以在类之间传递值?感谢您使用不同的键,如“HighScore1”、“PlayerLife”等。但是我使用singleton类来保存游戏统计数据…好的,但我不想存储数据。现在不行。我只想选择一场比赛(最后一场)的分数,然后把它传给下一层的比赛。游戏重新启动或应用程序关闭后,它可能会被释放!