Iphone 倒计时计数器/计时器

Iphone 倒计时计数器/计时器,iphone,objective-c,cocos2d-iphone,Iphone,Objective C,Cocos2d Iphone,请告诉我如何在iPhone的cocos2d中实现一个开始游戏的倒计时 我的意思是,按“播放”键时,一个新场景会显示数字“3”、“2”、“1”,然后显示单词“GO!”。如果需要使用cocos2d,请务必这样做,但是不使用cocos2d就更容易了。在IB中设置一个带有必要插座的UILabel,将countdownTimer声明为NSTimer对象,然后在ViewDiLoad或其他重要位置: countdownTimer = [NSTimer scheduledTimerWithTimeIn

请告诉我如何在iPhone的cocos2d中实现一个开始游戏的倒计时


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

如果需要使用cocos2d,请务必这样做,但是不使用cocos2d就更容易了。在IB中设置一个带有必要插座的UILabel,将
countdownTimer
声明为NSTimer对象,然后在ViewDiLoad或其他重要位置:

     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
     }
}
我还没有检查该代码的有效性,但它应该让你朝着正确的方向前进

来自“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:)];
...
关于游戏:

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

    }

}
播放:


:非常感谢你的回复。我会看看这一点,看看我如何利用它的cocos2d,因为这是我真正的追求。再次表示感谢。@zaki记住,如果你发现这是你所需要的,绿色大支票就在那里!:D:好吧,没问题,但我还在等着看是否会得到更多针对Cocos2d的回复或答案。非常感谢你详细的回答和对我问题的直接回答。它工作得很好!不要忘记在
init
方法的开头添加
if(self=[super init])
块。干杯
-(void) play {
    [[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInL transitionWithDuration:0.4 scene:[GamePlay node]]];
}