6

プレーヤーがゲームに入るとタイマーが 60 から 0 まで始まるように、単純なカウントダウン タイマーを作成しようとしています。

これまでのところ、GameController.m 内に次のようなメソッドを作成しました。

-(int)countDownTimer:(NSTimer *)timer {
    [NSTimer scheduledTimerWithTimeInterval:-1
                                 invocation:NULL
                                    repeats:YES];
    reduceCountdown = -1;
    int countdown = [[timer userInfo] reduceCountdown];
    if (countdown <= 0) {
        [timer invalidate];
    }
    return time;
}

ゲームの開始時に、整数の Time を 60 に初期化します。その後、ViewController 内でラベルが設定されます。しかし、コードをコンパイルすると、ラベルが 60 に表示されるだけで、まったく減少しません。

どんな助けでも大歓迎です-私はObjective-Cが初めてです。


編集

の助けを借りて、コードを 2 つの別々のメソッドに分割しました。コードは次のようになります。

-(void)countDown:(NSTimer *)timer {
    if (--time == 0) {
        [timer invalidate];
        NSLog(@"It's working!!!");
    }
}

-(void)countDownTimer:(NSTimer *)timer {
    NSLog(@"Hello");
    [NSTimer scheduledTimerWithTimeInterval:1
                                      target:self
                             selector:@selector(countDown:)
                                      userInfo:nil
                                      repeats:YES];
}

ただし、コードはまだ正しく実行されておらず、View Controller からメソッド [game countDownTimer] を呼び出すと、「認識されないセレクターがインスタンスに送信されました」と言って中断しますここで何が間違っているのか誰か説明できますか?

4

3 に答える 3

12

コードにいくつかの問題があります。

  • 時間間隔に間違ったパラメータを渡しています- 負の数は 0.1 ミリ秒として解釈されます
  • 間違ったオーバーロードを呼び出しています - 呼び出しオブジェクトを渡すことが期待されていますが、NULL
  • タイマーで実行するコードをタイマーの初期化と一緒に配置します。タイマーで実行する必要があるコードは、別のメソッドに入れる必要があります。

セレクターを受け取るオーバーロードを呼び出して1、間隔ではなく渡す必要があります-1

NSTimer *timerandを宣言してint remainingCountsから追加

timer = [NSTimer scheduledTimerWithTimeInterval:1
                                         target:self
                                       selector:@selector(countDown)
                                       userInfo:nil
                                        repeats:YES];
remainingCounts = 60;

カウントダウンを開始したい場所へ。次に、countDown メソッド自体を追加します。

-(void)countDown {
    if (--remainingCounts == 0) {
        [timer invalidate];
    }
}
于 2013-03-09T13:49:06.380 に答える
2

これを試して

- (void)startCountdown
{
    _counter = 60;

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                      target:self
                                                    selector:@selector(countdownTimer:)
                                                    userInfo:nil
                                                     repeats:YES];
}

- (void)countdownTimer:(NSTimer *)timer
{
    _counter--;
    if (_counter <= 0) { 
        [timer invalidate];
        //  Here the counter is 0 and you can take call another method to take action
        [self handleCountdownFinished];
   }
}
于 2013-03-09T13:51:27.030 に答える
1

あなたが提起した質問から、1秒ごとに関数を呼び出し、その中でデクリメントロジックを処理することでそれを達成できます。

スニペット:-

NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 1.0
                      target: self
                      selector:@selector(onTick:)
                      userInfo: nil repeats:YES];
(void)onTick
{
   //do what ever you want
   NSLog(@"i am called for every 1 sec");
//invalidate after 60 sec [timer invalidate];
}
于 2013-03-09T13:46:38.190 に答える