游戏结束并重启按钮Cocos2d

时间:2013-02-12 12:36:09

标签: cocos2d-iphone

我实施了一款游戏,在游戏结束时,游戏结束了。 所以我要暂停这一点,然后我要实现标签Game Over(Done),点和一个允许重启游戏的按钮。现在,当游戏结束时,我已经在游戏中叠加了标签和新的首发游戏!我希望在游戏结束和点数的视图中暂停游戏。

这是我的代码:

-(void)gameOver:(int)value punteggio:(id)punti{
    if (value == 1) {
        // partita vinta
    } 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 { 
            //  partita persa
            Gameover = [CCLabelTTF labelWithString:@"Game Over!" fontName:@"Marker Felt" fontSize:34];
            [Gameover setColor:ccc3(255, 1, 1)];
            Gameover.position = ccp(size.width / 2, size.height / 2);
            [self addChild:Gameover];
            //blinking
            id action1 = [CCBlink actionWithDuration:0.3 blinks:5];
            [Gameover runAction: action1];
            [[CCDirector sharedDirector] pause];
        }
    }
}

我该如何解决?如何让这个按钮在暂停的场景中重启游戏? 谢谢

2 个答案:

答案 0 :(得分:1)

你可以这样做:

使用CCMenuItemLabel创建一个简单的CCMenu。

      CCMenuItemLabel *gameOver = [CCMenuItemLabel itemWithLabel:@"Game Over!" target:self selector:@selector(restart:)];
      CCMenu *menu = [CCMenu menuWithItems:gameOver, nil];
      [self addChild:menu z:(Something bigger than all others so that it shows up on top)];

然后在你的重启方法中:

    -(void) restart:(id)sender{
          menu.visible = NO;
          //code to restart your game;
    }

希望这会有所帮助。

答案 1 :(得分:1)

我已经解决了创建一个名为GameOVer的节点并重启功能的问题。在GameOver实现中,我写了一个函数来重启

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

我已将此功能链接到GameOver图层中的标签:

CCMenuItemLabel *back = [CCMenuItemLabel itemWithLabel:label2  target:self selector:@selector(restart)];

最后,如果lives==0此代码

,我已连接到主图层
[[CCDirector sharedDirector] replaceScene:[GameOver node]];

一切正常! :)