【问题标题】:Three second countdown in cocos2dcocos2d中的三秒倒计时
【发布时间】:2013-10-07 05:26:02
【问题描述】:

我试图在我的游戏中通过一个关卡时显示 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 语句,它不起作用...

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

提前致谢!

【问题讨论】:

  • gameSegmentBeat 在哪里调用?我猜它被调用过一次。
  • 我更新了问题。

标签: iphone ios objective-c cocos2d-iphone


【解决方案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 并接受:)
【解决方案2】:

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

在更新循环中调用 gameSegmentBeat

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

    [self gameSegmentBeat];
}

【讨论】:

  • 是的,当我的游戏中的一个物体接触到另一个物体时,我调用了标签开关。我更新了问题。
【解决方案3】:

这个

[self pauseSchedulerAndActions];

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

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

【讨论】:

  • 你知道我可以用pauseSchedulerAndActions切换标签(或倒计时)的另一种方法吗?谢谢!
【解决方案4】:

几件事...

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

【讨论】:

    猜你喜欢
    • 2015-03-16
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 2016-03-25
    相关资源
    最近更新 更多