0

cocos2d でゲームのゲーム オーバー レイヤーに最終スコアを表示しようとすると問題が発生します。my variable の値を変更するアルゴリズムがあり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 incrementゲームオーバーレイヤーで自分の最終値を表示するにはどうすればよいですか? どうすればそれをクラスに渡すことができますか?

4

2 に答える 2

1

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];
于 2013-02-14T18:33:03.177 に答える
1

その場合は、スコアライブまたは同時に表示したいものを表示するために別のレイヤーを使用する必要があります。

HUD LayerieHeads-Up Displayクラスでは、画面に「あなたのスコア」、「あなたが勝つ」または「あなたが負ける」と表示し、その下に「再起動」と表示するボタンを表示するための基本的なコードを記述する必要がありCCLabelBMFontます。再起動ボタンがタップされると、ActionLayer スイッチの新しいインスタンスが作成されます。Score メソッドの scoreUpdate を記述します。このメソッドには、弾丸がモンスターに命中するたびにスコアを計算し、それをCCLabelBMFontラベルに更新するロジックが含まれます。あとは、そのメソッドを呼び出すだけです。

これは、そのような要件に最適なチュートリアルの 1 つです。

見る

于 2013-02-14T19:41:07.547 に答える