重启游戏后,cocos2d中的进度时间条消失

时间:2013-02-15 14:00:06

标签: cocos2d-iphone progress-bar

我创建了一个测验游戏,它实现了一个时间栏。在第一场比赛中没问题,但如果在比赛结束后,玩家点击“重启”,游戏就会正常进行,但时间栏会消失!

这里是我从GameOverLayer到Game的代码:

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

这里是创建新问题的功能

-(void)creaDomanda{

    //bar
    CCProgressFromTo *to1 = [CCProgressFromTo actionWithDuration:MaxTime from:100 to:0];
    bar = [CCProgressTimer progressWithFile:@"barra.png"];
    bar.type = kCCProgressTimerTypeHorizontalBarLR;
    [bar setPosition:ccp(size.width - 250 , size.height - 18)];

    int randomValue =  (arc4random() % 4) + 1; 
    NSString *stringa = [NSString stringWithFormat:@"Domanda%i", randomValue];
    dictionary = [plistData objectForKey:stringa];
    domanda = [dictionary valueForKey:@"Titolo"];
    labelDomanda = [CCLabelTTF labelWithString:domanda fontName:@"Marker Felt" fontSize:24];
    labelDomanda.position =  ccp( size.width /2 , 400 );
    [self addChild: labelDomanda];
    int rispostaEsatta = [[dictionary valueForKey:@"Soluzione"] intValue];
    menu = [CCMenu menuWithItems:nil];
    for (int i = 1; i<5;i++)
    {
        if(rispostaEsatta == i){
item = [CCMenuItemFont itemFromString:[dictionary valueForKey:
                                                               [NSString stringWithFormat:@"Risposta%i",i] ]
                                                       target:self selector:@selector(corretto)];
        }else{
            item = [CCMenuItemFont itemFromString:[dictionary valueForKey:
                                                               [NSString stringWithFormat:@"Risposta%i",i] ]
                                                       target:self selector:@selector(sbagliato)];
        }
        [menu addChild:item];
    }
//[..]
    [self addChild:menu];
    [self addChild:bar];
    [bar runAction:to1];
}

这里有一个正确/错误的方法(类似),毕竟,创建一个新问题:

-(void)sbagliato{
    CCLOG(@"Sbagliato");

    if (menu) [self removeChild:menu cleanup:YES];
    if (labelDomanda) [self removeChild:labelDomanda cleanup:YES];
    if (bar) [self removeChild:bar cleanup:YES];

    labelRisultato = [CCLabelTTF labelWithString:@"Hai sbagliato!" fontName:@"Marker Felt" fontSize:24];
    [labelRisultato setColor:ccc3(255, 1, 1)];
    labelRisultato.position = ccp(size.width / 2, 280);

    [self addChild:labelRisultato];
    [self gameOver:2 punteggio:0];
    // Richiamiamo il metodo per eliminare la label dopo 0,3 secondi
    [self performSelector:@selector(eliminaLabel) withObject:nil afterDelay:0.5];

    increment = increment - 20;
    [pointLabel setString: [NSString stringWithFormat: @"Punti: %i", increment]];

    // new question
    [self performSelector:@selector(creaDomanda) withObject:nil afterDelay:0.5];
}

任何人都可以向我解释为什么当我重新启动时间棒时会破坏者? 谢谢

1 个答案:

答案 0 :(得分:1)

我最好的猜测:

CCProgressFromTo操作仍在运行。由于它进展到0,因此CCProgressTimer最终不再显示它的任何部分。即使您对进度计时器运行另一个CCProgressFromTo操作,这可能会继续。

解决方案:确保在运行另一个CCProgressFromTo之前停止任何正在运行的CCProgressFromTo操作。

如果这不能解决问题,那么我认为需要通过将百分比设置回100来重置CCProgressTimer。