cocos2d三秒倒计时

时间:2013-10-07 05:26:03

标签: iphone ios objective-c cocos2d-iphone

当我的游戏中的等级被击败时,我正试图有3秒倒计时显示。所以这就是我所做的:

-(void) gameSegmentBeat {
    [self pauseSchedulerAndActions];

    // Overlay the game with an opaque background
    CCSprite *opaqueBG = [CCSprite spriteWithFile:@"background1.png"];
    opaqueBG.position = screenCenter;
    [self addChild:opaqueBG z:10000];

    // These are the 3 labels for the 3 seconds
    CCLabelTTF *three = [CCLabelTTF labelWithString:@"3" fontName:@"Helvetica" fontSize:100];
    three.position = ccp(screenCenter.x, screenCenter.y);
    CCLabelTTF *two = [CCLabelTTF labelWithString:@"2" fontName:@"Helvetica" fontSize:100];
    two.position = ccp(screenCenter.x, screenCenter.y);
    CCLabelTTF *one = [CCLabelTTF labelWithString:@"1" fontName:@"Helvetica" fontSize:100];
    one.position = ccp(screenCenter.x, screenCenter.y);

    // Secondspast is specified in the update method 
    secondspast = 0;
    if (secondspast == 1) {
        [self addChild:three z:10001];
    } else if (secondspast == 2) {
        [self removeChild:three];
        [self addChild:two z:10001];
    } else if (secondspast == 3) {
        [self removeChild:two];
        [self addChild:one z:10001];
    }
}

更新:

framespast = 0;

-(void)update:(ccTime)delta {
      framespast++;
      secondspast = framespast / 60;
}

我在上面没有显示的方法中调用了gameSegmentBeat ...我知道它被调用是因为:1。pauseSchedulerAndActions正在运行,而CCSprite *opaqueBG正在调用[self addChild:three z:10001];正在展示。

另外,我应该添加的是,如果我将{{1}}移到当前所在的if语句之外,那么标签会显示,但是一旦我回到if语句中,它不起作用......

我不知道为什么,但标签没有每秒切换,因此没有倒计时。

提前致谢!

4 个答案:

答案 0 :(得分:1)

您可能会使用定时执行块执行1秒,2秒和3秒的延迟:

dispatch_time_t countdownTime1 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC));
dispatch_after(countdownTime1, dispatch_get_main_queue(), ^(void){

    [self addChild:three z:10001];

});

dispatch_time_t countdownTime2 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC));
dispatch_after(countdownTime2, dispatch_get_main_queue(), ^(void){

    [self removeChild:three];
    [self addChild:two z:10001];

});

dispatch_time_t countdownTime3 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC));
dispatch_after(countdownTime3, dispatch_get_main_queue(), ^(void){

    [self removeChild:two];
    [self addChild:one z:10001];

});

答案 1 :(得分:0)

您的标签未切换,因为未调用切换标签的代码(至少,它在您共享的代码中不可见)。

从更新循环中调用gameSegmentBeat

-(void)update:(ccTime)delta {
    framespast++;
    secondspast = framespast / 60;

    [self gameSegmentBeat];
}

答案 2 :(得分:0)

[self pauseSchedulerAndActions];

将阻止调用update方法。正如它所说,这会暂停调度程序。

测试更新是否被调用set a breakpoint

答案 3 :(得分:-1)

很少......

  1. 函数gameSegmentBeat被调用一次,其中secondspast的值设置为0,因此没有if-conditions被满足,因此你看不到任何标签。

  2. 函数gameSegmentBeat调用pauseSchedulerAndActions暂停所有计划的选择器和操作,因此,您的函数update未被调用,因此secondspast的值不会更改。即使它确实发生了变化,但在上面的代码中并不重要,因为没有从更新中调用gameSegmentBeat。

  3. 现在,如果你必须调用pauseSchedulerAndActions,你可以执行以下操作:

    -(void) visit{
    
        framespast++;
        secondspast = framespast / 60;
    
        if (secondspast == 1) {
            [self addChild:three z:10001];
        } else if (secondspast == 2) {
            [self removeChild:three];
            [self addChild:two z:10001];
        } else if (secondspast == 3) {
            [self removeChild:two];
            [self addChild:one z:10001];
        }
        [super visit];
    }