2011-10-24 19 views

回答

0

我會去使用NSOperation睡覺和添加點(實際上它可能即使沒有睡覺)。喜歡的東西:

NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self 
                   selector:@selector(updateScore) 
                    object:nil]; 
[queue addOperation:[op autorelease]]; 

-(void)updateScore { 
    while (tot < 1.0) { 
    displayingScore += score*incr; 
    tot += incr; 
    // update score label to displayingScore 
    [NSThread sleepForTimeInterval:0.05]; 
    } 
} 
+0

這不是太有點矯枉過正了嗎?計時器也應該工作得很好。 – JustSid

+0

我習慣於編寫應該避免正常iOS時間的代碼(Cocos2D),這就是爲什麼我通常更喜歡這種方式 – Jack

2

創建UILabel顯示得分,並使用NSTimer更新UILabeltext財產。蘋果公司並沒有發佈一個能爲你做這一切的課程。

1

您需要兩個變量;顯示currentScore和當前得分。您還需要一個定時器來處理更新ui。

當您的分數發生變化時,將currentScore更新爲您想要的最終分數。然後啓動一個計時器,以增加顯示的分數,直到它到達那裏,即

-(void)scoreUpdater:(NSTimer *)timer { 
    // Update the score 
    displayScore ++; 
    [scoreLabel setText:[NSString stringWithFormat:@"%i", displayScore]]; 

    // Have we got there yet? 
    if (displayScore == currentScore) { 
     [scoreTimer invalidate]; 
     [scoreTimer release]; 
     scoreTimer = nil; 
    } 
} 
相關問題