2011-11-19 98 views
2

请任何人都可以告诉我如何实现一个倒计时器,用于iPhone的cocos2d游戏。倒数计数器/定时器

我的意思是,在按下“播放”一个新场景后,会出现数字“3”,“2”,“1”,然后显示单词“GO!”。

回答

3

从 “cocos2d的最佳实践”:

尽量不使用Cocoa的的NSTimer。而是使用cocos2d自己的调度程序。

所以这是使用cocos2d的调度器为您的标签设置动画的示例,即使有一些效果。

在@interface:

int timeToPlay; 
CCLabelTTF * prepareLabel; 
CCLabelTTF * timeoutLabel; 
CCMenu *menu; 

在INIT:

timeToPlay=4; 

CGSize s = [CCDirector sharedDirector].winSize; 
prepareLabel = [CCLabelTTF labelWithString:@"Prepare to play!" fontName:@"Marker Felt" fontSize:40]; 
prepareLabel.position = ccp(s.width/2.0f, 150); 

timeoutLabel = [CCLabelTTF labelWithString:@"3" fontName:@"Marker Felt" fontSize:60]; 
timeoutLabel.position = ccp(s.width/2.0f, 90); 

[self addChild:prepareLabel]; 
[self addChild:timeoutLabel]; 

timeoutLabel.visible=NO; 
prepareLabel.visible=NO; 

... 
CCMenuItem *Play = [CCMenuItemFont itemFromString:@"PLAY" 
              target:self 
             selector:@selector(aboutToPlay:)]; 
... 

aboutToPlay:

-(void) aboutToPlay: (id) sender { 
    [self removeChild:menu cleanup:YES]; 
    timeoutLabel.visible=YES; 
    prepareLabel.visible=YES; 
    [self schedule: @selector(tick:) interval:1]; 
} 

和蜱:

-(void) tick: (ccTime) dt 
{ 
    if(timeToPlay==1) [self play]; 
    else { 
     timeToPlay--; 
     NSString * countStr; 

     if(timeToPlay==1) 
     countStr = [NSString stringWithFormat:@"GO!"]; 
     else 
     countStr = [NSString stringWithFormat:@"%d", timeToPlay-1]; 

     timeoutLabel.string = countStr; 

     //and some cool animation effect 
     CCLabelTTF* label = [CCLabelTTF labelWithString:countStr fontName:@"Marker Felt" fontSize:60]; 

     label.position = timeoutLabel.position; 
     [self addChild: label z: 1001]; 
     id scoreAction = [CCSequence actions: 
          [CCSpawn actions: 
           [CCScaleBy actionWithDuration:0.4 scale:2.0], 
           [CCEaseIn actionWithAction:[CCFadeOut actionWithDuration:0.4] rate:2], 
           nil], 
          [CCCallBlock actionWithBlock:^{ 
           [self removeChild:label cleanup:YES]; 
          }], 
          nil]; 
     [label runAction:scoreAction]; 

    } 

} 

播放:

-(void) play { 
    [[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInL transitionWithDuration:0.4 scene:[GamePlay node]]]; 
} 
+0

非常感谢您的详细解答并直接回答我的问题。它完美的工作! – Zaki

+0

不要忘记在'init'方法的开头添加'if(self = [super init])'块。干杯 – hyd00

2

如果你需要使用cocos2d,尽量做到这一点,但它会更容易做到这一点没有。建立一个UILabel与IB必要的网点,声明countdownTimer作为一个NSTimer对象,然后在您的viewDidLoad中或其他地方显著:

 countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; 
    label.text = @"3"; 
    [countdownTimer fire]; 

,然后再更新时间:

- (void)updateTime { 
    if ([label.text isEqualToString:@"3"]) { 
      label.text = @"2"; 
    } else if ([label.text isEqualToString:@"2"]) { 
      label.text = @"1"; 
    } else { 
      label.text = @"GO!"; 
      [countdownTimer invalidate]; 
      //continue with app 
    } 
} 

避风港”检查代码的有效性,但它应该让你朝着正确的方向前进!

+0

:非常感谢您的回复。我会看看这个,看看我可以如何将它用于cocos2d,因为这是我真正的追求。再次感谢一百万。 – Zaki

+2

@zaki记住,如果你发现它是你需要的,那么大绿色检查就在那里! :D –

+0

:好的不是问题,但我仍然在等待,看看我是否会得到更多答案或特定于Cocos2d的答案。谢谢 – Zaki