質問

ゲームにカウントダウンタイマーがあり、テーブルに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];
役に立ちましたか?

解決

サブセカンドの更新を行うには、タイマーの間隔を<1にする必要がありますが、NSTIMERの精度はわずか50ミリ秒であるため、 scheduledTimerWithTimeInterval:0.01 動作しないでしょう。

さらに、タイマーはさまざまなアクティビティによって遅れる可能性があるため、 timeSeconds 不正確なタイミングにつながります。通常の方法は、NSDateを今すぐNSDATEと比較することです。タイマーが起動する日付です。ただし、このコードはゲーム用であるため、現在のアプローチにより、プレイヤーへの不満が少なくなる可能性があります。プログラムまたはバックグラウンドプロセスが多くのリソースを消費する場合。


最初にすることは、CountDownTimerをサブ秒間隔に変換することです。

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

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

double timeSeconds;
...
if(appDelegate.gameStateRunning == YES){
   if(gameVarLevel==1){
      timeSeconds = 100;
      AllowResetTimer = NO;
   }
}
timeSeconds -= 0.67;
timerLabel.text=[NSString stringWithFormat:@"Time: %.2g", timeSeconds];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top