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];
}
我个人不知道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
}
}
我猜你是要求视觉显示柜台从零开始的高分? –