2016-12-24 6 views
1

次の関数は、赤色から緑色のライトをカウントダウンし、緑色のライトが表示された後にユーザーがボタンを押す反応時間をカウントします。swift関数のネストされたif文

func updateCounter() { 

    timerInt -= 1 
    if timerInt == 2{ 
     light.image = UIImage(named: "r.png") 
    } else if timerInt == 1 { 
     light.image = UIImage(named: "yellow.png") 


    } else if timerInt == 0 { 

     light.image = UIImage(named: arc4random_uniform(2) == 0 ? "no.png" : "g.png") 


     timer.invalidate() 
     startStop.isEnabled = true 
     scoreTimer = Timer.scheduledTimer(timeInterval: 0.0001, target: self, selector: #selector(ViewController.updateScoreTime), userInfo: nil, repeats: true) 

    } 
} 

else if timerInt == 0の場合、ユーザにはランダムな機能が与えられます。画像が緑色に変わるか、「x」が表示されます。

xが表示されている場合、赤いライトシーケンスを再開するにはゲーム終了を示すボタンを押す必要があります。

緑のランプが表示されている場合は、反応時間をテストする必要があります。

これは、xが表示されても変更されない点を除き、関数はすでに実行されています。私は次のように私は実行するための機能をご希望の推測:

timeInt == 0と緑色の光が
を選択した場合timeInt == 0とxが
選択された場合、その後のテストの反応時間に

を実行します 終了反応時間とボタン上でゲームを実行

どうすればいいですか?

+2

0.0001あなたのタイマーはリフレッシュレートが速すぎます。毎秒10,000回実行する必要があります。ほとんどのディスプレイの更新に必要な時間の1/30を使用してください。 –

+0

あなたの質問はあまりにも一般的です。たぶんあなたは私たちにもう少し文脈を与えることができます。どのように反応時間をテストするのですか?どのように反応時間を終了するのですか? updateScoreTime()とは何ですか? –

答えて

0

私はあなたが達成しようとしていることを正確には分かっていないので、ベストを尽くしました。これを試してみてください:

func updateCounter() { 

    timerInt -= 1 
    if timerInt == 2{ 
     light.image = UIImage(named: "r.png") 
    } else if timerInt == 1 { 
     light.image = UIImage(named: "yellow.png") 
    } else if timerInt == 0 { 
     let green = (arc4random_uniform(2) == 0) 

     light.image = UIImage(named: (green ? "g.png" : "no.png")) 

     if green { 
      // run test reaction time 
     } else { 
      //end reaction time and run game over button 
     } 

     // NOTE: Do you mean `scoreTimer.invalidate()`? 
     timer.invalidate() 
     startStop.isEnabled = true 
     // NOTE: This time interval seems WAY too small ↓↓↓↓↓↓: it is only a millisecond! 
     scoreTimer = Timer.scheduledTimer(timeInterval: 0.0001, target: self, selector: #selector(ViewController.updateScoreTime), userInfo: nil, repeats: true) 

    } 
} 

適切なコードで(//で始まる行別名)最初の2つのコメントを元に戻し、第3および第4のコメントをメモします。

関連する問題