TheInterestedOne
TheInterestedOne

Reputation: 768

How to display the score in the game over layer in cocos2d

I've a problem when I try to show the final score in the Game Over Layer in a game in cocos2d. There's an algorithm that modify the value of my variable increment, that contains the points of the user and then the game shows them.

increment = increment + 50;
[pointLabel setString: [NSString stringWithFormat: @"Points: %i", increment]];

and then a function controls if the user has or not lives in his play

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

Then the GameOverLayer is called. This is the .h file

@interface GameOver : CCNode { 
    CGSize size;
    CCLabelTTF *label1;
    CCLabelTTF *label2;
    CCLabelTTF *labelpnt;
    CCLabelTTF *labelscore;
}
-(void)restart;

Here the .m file

@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]];
}

How can I show the final value of my int increment in the game over layer? How can I pass it throught the classes?

Upvotes: 0

Views: 886

Answers (2)

Mohammad Kamar Shad
Mohammad Kamar Shad

Reputation: 6067

Hey in that Case You should take a separate layer for displaying the Score lives or whatever you want to display simultaneously.

In HUD Layer i.e Heads-Up Display class You should write some basic code to display a CCLabelBMFont to the screen that says "Your Score", “You Win” or “You Lose” and a button underneath that says “Restart”. When the restart button is tapped, it creates a new instance of the ActionLayer switches to it. Write a Score method scoreUpdate, this method will have the logic for the score calculation like whenever a bullet hit to monster and update it to the CCLabelBMFont label. And then All you need to do just call that method.

Here is the one of the best tutorial for such requirement.

See

Upvotes: 1

Guru
Guru

Reputation: 22042

Use UserDefault. Here is code

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

Upvotes: 1

Related Questions