2
A
回答
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]]];
}
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
}
}
避风港”检查代码的有效性,但它应该让你朝着正确的方向前进!
相关问题
- 1. jquery定时器或倒数计时器
- 2. 倒数计时器
- 3. 倒数计时器
- 4. 倒数计时器
- 5. 倒数计时器?
- 6. 修复倒计时倒数计时器
- 7. 倒计时 - iPhone倒数计时器
- 8. 倒数计时器在PHP
- 9. JavaScript倒数计时器
- 10. 倒数计时器C#
- 11. 5秒倒数计时器
- 12. Android倒数计时器
- 13. 使用倒数计时器
- 14. jQuery的倒数计时器
- 15. 倒数计时器问题
- 16. Javascript倒数计时器
- 17. 随机倒数计时器
- 18. 如何倒数计时器
- 19. PHP倒数计时器
- 20. 倒数计时器迅速
- 21. 流星:倒数计时器
- 22. iPhone sdk:倒数计时器
- 23. 倒数计时器通知
- 24. 倒数计时器(JS)
- 25. 倒数计时器问题
- 26. 可定义的倒数计时器
- 27. 倒数计时器开始计时
- 28. WPF定时器倒计时
- 29. VB定时器倒数
- 30. 从mysql的倒计时器定时器
非常感谢您的详细解答并直接回答我的问题。它完美的工作! – Zaki
不要忘记在'init'方法的开头添加'if(self = [super init])'块。干杯 – hyd00