Из "Cocos2d Best Practices":
Старайтесь НЕ использовать какао 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];
}
}
Play:
-(void) play {
[[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInL transitionWithDuration:0.4 scene:[GamePlay node]]];
}