1

-- Edit --

I cannot answer my own question, but I will leave my solution here in this edit.

Thanks everyone for your time in looking at my problem.

I am creating 1 new variable

var timer = 1

I am using this in my Async dispatch timer for the seconds, and incrementing it by 1 each round to space the sequence out, it took a lot of thinking, but this solves my problem.

I wanted the buttons to be played in a sequential order, the solution then is to adjust the dispatch timer per the below.

Thank you everyone that commented, it got me thinking differently and helped me to solve the problem.


I am new to programming and to Swift and have been trying to make a small program but I have been stumped on a problem with loops, I am really new to swift and have tried "for loop", repeat-while, while loops, etc. The issue I have is I am trying to get 4 UI elements "Alphas" to change in the order that a new value is created but currently it seems to happen so fast that all 4 buttons just blink out at the same time.

What I am doing is a there are 4 colored tiles, I am using single value array generated randomly and on each pass of the loop I am appending the newly generated array value of "newChallenge" to an array called "challenge" which the user will need to input to get a correct result from the game.

I was trying to add playback and there is a function called "showChallenge", I convert the newChallenge value to an integer and use a switch hoping that each of the buttons would flash in order of the newChallenge being created. However what I am getting is that all 4 tiles will just flash at once, I have tried using async to create a timing between them but I am missing something. Can someone let me know how to approach this or show me where I am going wrong in my code?

there are a few other variables but they are self explanatory, if someone could help me out it would be great. This is my first Swift program, and as fun as it has been I have spent a lot of hours trying to solve this problem. I would appreciate any help I can get :)

func challenges() {
    var low = 1
    var high = 5

    while low != high {
        newChallenge = [Int.random(in: 1..<5)]
        showChallenge()
        challenge.append(contentsOf: newChallenge)
        low += 1
     }
     tmpChallenge.text = "Array: \(self.challenge)" //debugging
}

func showChallenge () {
    DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
        self.uTurnPrompt.isHidden = false
        self.low = 1
    }        
    var btnFeedback = newChallenge[indexElement]
    uTurnPrompt.isHidden = true

    switch ("JRSM" == "JRSM") {
        case btnFeedback == 1:
            print("num1")
            self.btn_Num1.alpha = 0.1
            DispatchQueue.main.asyncAfter(deadline: .now() + (btnSpeed)) {
                self.btn_Num1.alpha = 1
            }
            break;
        case btnFeedback == 2:
            print("num2")
            self.btn_Num2.alpha = 0.1
            DispatchQueue.main.asyncAfter(deadline: .now() + (btnSpeed)) {
                self.btn_Num2.alpha = 1
            }
            break;
        case btnFeedback == 3:
            print("num3")
            self.btn_Num3.alpha = 0.1
            DispatchQueue.main.asyncAfter(deadline: .now() + (btnSpeed)) {
                self.btn_Num3.alpha = 1
            }
            break;
        case btnFeedback == 4:
            print("num4")
            self.btn_Num4.alpha = 0.1
            DispatchQueue.main.asyncAfter(deadline: .now() + (btnSpeed)) {
                self.btn_Num4.alpha = 1
            }
            break;
        default:
            break;
        }

    infoText.isHidden = false
    DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
        self.infoText.isHidden = true
    }
}
  • What is `btnSpeed`? – Wyetro Feb 25 '20 at 20:17
  • What is `indexElement`? And the `switch` argument is pointless, it's not related to the cases at all. – vadian Feb 25 '20 at 20:20
  • the indexelement is the index 0 of the array, the switch argument is pointless, that is not the issue I used anything because swift demanded an argument, if the value of the index is 1, it uses case 1 and affects a button which is Num1, ad so on. –  Feb 25 '20 at 20:28
  • btnSpeed is the value for the delay, it is set to 1 at the moment –  Feb 25 '20 at 20:30
  • 1
    The proper usage of the switch is `switch btnFeedback { case 1: .... }` – vadian Feb 25 '20 at 20:42
  • You should look at UIView animation rather than trying to roll your own with asynchronous dispatch – Paulw11 Feb 25 '20 at 22:22

1 Answers1

0

The issue is that you're calling showChallenge in the loop. Everything is called synchronously and so it's going to appear that it's all being called at the same time.

Modify calling showChallenge in the loop by wrapping it in a delay based on the order you're calling it, like this:

DispatchQueue.main.asyncAfter(deadline: .now() + low) {
    showChallenge()
}
Wyetro
  • 8,439
  • 9
  • 46
  • 64
  • Hello Wyetro, the showChallenge is part of the loop because I am generating a single value each time before appending to the challenge, this is sending to the switch, and the printing of the numbers works, in sequence, it is the alpha changes that are not working, they are in the same switch. I had tried this previously, then what happens is that only the last button flashes, (I cannot win). ;) –  Feb 25 '20 at 20:46
  • @SteveInCA did you try what I suggested, using `.now() + low` as the delay? – Wyetro Feb 26 '20 at 00:01
  • @SteveInCA glad this worked — mark it as correct if it worked – Wyetro Feb 26 '20 at 21:23