3

ゲームにカウントダウンタイマーがあり、テーブルに小数点以下2桁と、小数点以下2桁のレコードが表示されるようにする方法を見つけようとしています。現在、整数としてカウントダウンし、整数として記録します。何か案は?

-(void)updateTimerLabel{

     if(appDelegate.gameStateRunning == YES){

                            if(gameVarLevel==1){
       timeSeconds = 100;
       AllowResetTimer = NO;
       }
    timeSeconds--;
    timerLabel.text=[NSString stringWithFormat:@"Time: %d", timeSeconds];
}

    countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTimerLabel) userInfo:nil repeats:YES];
4

1 に答える 1

2

1秒未満の更新を行うには、タイマーの間隔を1未満にする必要があります。ただし、NSTimerの精度は約50ミリ秒であるため、機能しscheduledTimerWithTimeInterval:0.01ません。

また、さまざまな動作によりタイマーが遅れる場合がありますので、使用timeSecondsするとタイミングが不正確になります。通常の方法は、NSDateをタイマーの開始日と比較することです。ただし、このコードはゲーム用であるため、現在のアプローチでは、特にプレーヤーのフラストレーションが少なくなる可能性があります。プログラムまたはバックグラウンドプロセスが大量のリソースを消費する場合。


最初に行うことは、countdownTimerを1秒未満の間隔に変換することです。

countdownTimer = [NSTimer scheduledTimerWithTimeInterval:0.67 target:self selector:@selector(updateTimerLabel) userInfo:nil repeats:YES];

次に、時間を秒単位でカウントダウンするのではなく、センチ秒単位でカウントダウンします。

if(appDelegate.gameStateRunning == YES){
   if(gameVarLevel==1){
      timeCentiseconds = 10000;
      AllowResetTimer = NO;
   }
}
timeCentiseconds -= 67;

最後に、出力で100で除算します。

timerLabel.text=[NSString stringWithFormat:@"Time: %d.%02d", timeCentiseconds/100, timeCentiseconds%100];

または、:を使用しdoubleます

double timeSeconds;
...
if(appDelegate.gameStateRunning == YES){
   if(gameVarLevel==1){
      timeSeconds = 100;
      AllowResetTimer = NO;
   }
}
timeSeconds -= 0.67;
timerLabel.text=[NSString stringWithFormat:@"Time: %.2g", timeSeconds];
于 2010-02-13T06:17:59.980 に答える