vendredi 30 décembre 2016

how to put a 2 button condition in a game in swift 3

My code below is a traffic light game. When the light goes green the user has 6 seconds to to hit a button to respond to the green light. If the user does not hit the button within 6 seconds the game goes to the game over scene. On the 4th loop of the game I would like the user to have to hit 2 of the button( which in the code is button xq and startStop) within 6 seconds if not the user is taken to the game over scene. Right now in the game during the 4th loop the game is automatically taken to the game over scene. I have heard about possibly using the .isSeletected command I am not sure if that is useful.

 import UIKit
     class ViewController: UIViewController {
   @IBOutlet var light: UIImageView!
   @IBOutlet var labzel: UILabel!
   @IBOutlet var startStop: UIButton!
   @IBOutlet var xq: UIButton!
     let button = UIButton()


 var timer = Timer()
 var scoreTimer = Timer()
 var s = Timer()

var timerInt = 0
var secondsOfTimer = 0
var sucessfullCompletionofOneTimeOfGame = 0

override func viewDidLoad() {
    super.viewDidLoad()
    secondsOfTimer = 0
    labzel.text = String(secondsOfTimer)

}

@IBAction func hitTheButton(_ sender: AnyObject) {
    if secondsOfTimer == 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("Hit", for: [])
        secondsOfTimer = 0
        labzel.text = String(secondsOfTimer)
    } else {
        scoreTimer.invalidate()

    }
    if timerInt == 0 {
        secondsOfTimer = 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")


            if sucessfullCompletionofOneTimeOfGame > 3
            {
                timer.invalidate()
                xq.isEnabled = true
                xq.alpha = 1.0
                startStop.isEnabled = true
                scoreTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.updateScoreTime), userInfo: nil, repeats: true)
                if self.xq.isSelected && self.startStop.isSelected {
                    return
                } else {
                    endGame()
                }
        }


            else {

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


    }}
    func endGame(){
        let next = self.storyboard?.instantiateViewController(withIdentifier: "tViewController") as? tViewController
        self.present(next!, animated: true, completion: nil)

    }
func updateScoreTime(){
    secondsOfTimer += 1
    labzel.text = String(secondsOfTimer)

    if secondsOfTimer <= 6 {
        sucessfullCompletionofOneTimeOfGame +=  1
        return
    }
    else {
    endGame()
    }}}

Aucun commentaire:

Enregistrer un commentaire