My code below is a traffic light that when the light hits green. The user is timed. Right now the user has 3 seconds to hit the button. If the user does not hit the button within 3 seconds the user goes to game over scene. If the user successfully hits the button within 3 seconds the user has another 3 seconds to hit the button. How can I subtract .1 second every time the user successfully hits the button? All of my code is below.
import UIKit
class ViewController: UIViewController {
@IBOutlet var light: UIImageView!
@IBOutlet var labzel: UILabel!
@IBOutlet var startStop: UIButton!
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: [])
}}
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)
if scoreInt <= 3{
return
} else {
let next = self.storyboard?.instantiateViewController(withIdentifier: "tViewController") as? tViewController
self.present(next!, animated: true, completion: nil)
}
}}