My code is a streetlight that goes from red → yellow → green and times your reaction time when the light goes green. I want the user to switch view controllers to the game over view controller (automatically) if the user does not press the button within 3 seconds. I know how to segue the view controller so I don’t need the code to that. I just need to know how to do a nested if loop within the update counter function. So that the user can either play the game again on go to the game over scene. If else options depending on weather the users reaction time was las than< 3 than seconds or greater than or equal to 3 seconds.
var timer = Timer()
var scoreTimer = Timer()
var timerInt = 0
var scoreInt = 0
override func viewDidLoad() {
super.viewDidLoad()
scoreInt = 0
labzel.text = String(scoreInt)
}
@IBAction func hitTheButton(_ sender: AnyObject) {
if scoreInt == 0{
timerInt = 3
light.image = UIImage(named: "r.png")
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.updateCounter), userInfo: nil, repeats: true)
startStop.isEnabled = false
startStop.setTitle("Restart", for: [])
scoreInt = 0
labzel.text = String(scoreInt)
} else {
scoreTimer.invalidate()
}
if timerInt == 0 {
scoreInt = 0
startStop.setTitle("Restart", for: [])
}}
// This is where the game is counted down from red to yellow to green.
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: "g.png")
timer.invalidate()
startStop.isEnabled = true
scoreTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.updateScoreTime), userInfo: nil, repeats: true)
}}
func updateScoreTime(){
scoreInt += 1
labzel.text = String(scoreInt)
}}