0

当我的游戏中一个关卡被击败时,我试图显示 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正在工作,2.CCSprite *opaqueBG正在显示。

另外,我应该补充的是,如果我移到[self addChild:three z:10001];它当前所在的 if 语句之外,那么标签就会显示出来,但是一旦我移回 if 语句,它就不起作用了......

我不知道为什么,但是标签并不是每秒都在切换,所以没有倒计时。

提前致谢!

4

4 回答 4

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

});
于 2013-10-08T00:16:38.097 回答
0

your label is not switched as the code for switching the label is not called (at least, it's not visible in the code you shared).

invoke gameSegmentBeat from within your update loop

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

    [self gameSegmentBeat];
}
于 2013-10-07T07:22:40.700 回答
0

这个

[self pauseSchedulerAndActions];

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

要测试是否调用 update设置断点

于 2013-10-07T09:04:14.940 回答
-1

一些事情...

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

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

现在,如果您必须调用 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];
}
于 2013-10-08T05:15:14.543 回答