2013-10-18 94 views
0

如何製作計數器,將從零增加(貫穿)至兩秒內達到的分數?我打算用這個在彈出窗口中顯示遊戲中的最終分數。我不太確定如何去做這件事。請幫忙。爲分數製作增量計數器

+0

我猜你是要求視覺顯示櫃檯從零開始的高分? –

回答

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