Animations not always showing

Hi everyone, a relative newbie here looking for advice and a simple solution.

We are trying to create a memory game based on flashing colours, basically, when the game starts it will flash 1 of 4 buttons, and store the number of the button in an array, eventually, when we get to the coding for this part we well tap the relevant buttons in the correct order.

The issue that I am having if for say the buttons are 3, 1, 2, 0 it will flash them all correctly, but if the sequence is 3, 1, 1, 0 it will flash 3 and then 1, longer delay than expected and then flash 0

The relevant part of code is

func randomNumbers() {
    memoryColour.append(Int.random(in: 0 ..< 4))
    let numberOfGoes = memoryColour.count - 1
    for flash in 0...numberOfGoes {
        delaySec = Double(Double(flash) * 1.025)
        displayOrder(sender1: allButtons[memoryColour[flash]], colour3: colour4[memoryColour[flash]] )
    }
    //        print(memoryColour)
}

func displayOrder(sender1: UIButton, colour3: UIColor ) {
    UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 0.5, delay: TimeInterval(delaySec), options: [.autoreverse], animations: {sender1.backgroundColor = .black}, completion: { _ in
        sender1.backgroundColor = colour3
    })
}

Any suggestions appreciated

Thank you

oh so you want to skip same number flash?, then i suggest putting an if, have a variable like lastFlash

then check if lastFlash != currentFlash so you dont need to call displayOrder if the condition is false

Hi, sorry for the long delay in responding it’s a crazy world we are living in

What I actually want to do is the opposite of your suggestion. if button 1 appears 3 times in a row in the random generation, then the button will flash 3 times, with m code it only flashes once.

Thanks for your help

hmm. so the number of times you click on it will be the number of times it flashes?, it will always flash immediately after every click though. but you can modify the number of animations by probably using a delayed dispatchqueue

DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) { // Change `2.0` to the desired number of seconds.
   // Code you want to be delayed
}

so something like

func displayOrder(sender1: UIButton, colour3: UIColor ) {
    UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 0.5, delay: TimeInterval(delaySec), options: [.autoreverse], animations: {sender1.backgroundColor = .black}, completion: { _ in
        sender1.backgroundColor = colour3
    })
if(numclicks!=numflashes){
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5 ) { // since the delay of your animation is 0.5
   randomNumbers() 
}
}

}

this will get very very confusing for you as this is not even the complete logic of it, but it should be a idea to start with

Hello again, that looks very complex for a newcomer like me, but I will try and give it a go.

My problem is even before pressing the buttons, if it generates flash 1 as button 1, it will flash button 1, if then the random number is again flash 1 as button 1, so the sequence so far is 1 & 1, it will only flash button 1 once

I can see that in the code you have provided it does introduce a delay so I can see that that may work.

Thanks for your help I will give it a go

1 Like

hello heres an update, i dont think you would need to learn or use async at all, maybe use timers instead. here is an article we have on it, you should be able to fashion it to your needs to “delay” the flashing