jeudi 15 mars 2018

Swift delay after if is true

I am currently learning Swift and can not continue. I create a dice game. If the "if query" is true, the program should pause briefly before a new query is possible.

@IBAction func guess(_ sender: Any) {
    let diceRoll = arc4random_uniform(5)
    let fingerErgebnis = diceRoll + 1
    let fingerAntwort = String(fingerErgebnis)

    if fingerTextfield.text == fingerAntwort{
        ergebnis.text = "Richtig erraten!"
        ergebnis.textColor = UIColor.green
        ergebnis.font = ergebnis.font.withSize(20)
        bild.image = UIImage(named: ("win.jpg"))
        button.setTitle("Neues Spiel!", for: .normal)
        fingerTextfield.text = " "
        sleep (2)



    }else if fingerErgebnis == 1 {
        ergebnis.text = "Leider falsch! Die Zahl war \(fingerAntwort)."
        ergebnis.textColor = UIColor.red
        bild.image = UIImage(named: ("finger1.jpg"))
        button.setTitle("Versuch es nochmal!", for: .normal)

    } ...

As far as everything works, but I want that everything is running first and I have to wait 2 seconds until I can click the button again. My test is paused first, then the rest of the if commands are executed. I want that the other way around.

Sorry for my terrible english ;)

Aucun commentaire:

Enregistrer un commentaire