我怎样才能制作一个计数器,它将在两秒内从零递增(运行)到获得的分数?我打算用它来显示游戏中的最终分数。我不太确定该怎么做。请帮忙。
问问题
185 次
2 回答
0
我个人不知道 cocos2d 以及它如何显示文本或使用计时器,但这里是如何使用纯 iOS SDK 做到这一点。如果你知道 cocos2d,转换它应该不是问题。
- (void)viewDidLoad
{
[super viewDidLoad];
highScoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 100.0, 200.0, 75.0)];
[self displayHighScore];
}
-(void)displayHighScore {
highScore = 140;
currentValue = 0;
NSString* currentString = [NSString stringWithFormat:@"%d", currentValue];
[highScoreLabel setText:currentString];
[self.view addSubview:highScoreLabel];
int desiredSeconds = 2; //you said you want to accomplish this in 2 seconds
[NSTimer scheduledTimerWithTimeInterval: (desiredSeconds/highScore) // this allow the updating within the 2 second range
target: self
selector: @selector(updateScore:)
userInfo: nil
repeats: YES];
}
-(void)updateScore:(NSTimer*)timer {
currentValue++;
NSString* currentString = [NSString stringWithFormat:@"%d", currentValue];
[highScoreLabel setText:currentString];
if (currentValue == highScore) {
[timer invalidate]; //stop the timer because it hit the same value as high score
}
}
于 2013-10-18T14:00:47.623 回答
0
以下是可用于根据给定值设置动画(使用调度程序)的代码:
float secs = 2.0f;
float deciSecond = 1 / 10;
newScore = 100;
currentScore = 0;
scoreInDeciSecond = (newScore / secs) * deciSecond;
[self schedule:@selector(counterAnimation) interval:deciSecond];
这就是您的方法将如何处理动画:
- (void)counterAnimation {
currentScore += scoreInDeciSecond;
if (currentScore >= newScore) {
currentScore = newScore;
[self unschedule:@selector(counterAnimation)];
}
scoreLabel.string = [NSString stringWithFormat:@"%d", currentScore];
}
于 2013-10-18T13:52:15.987 回答