2

Im trying to loop through an array, lighting up one color at a time, and once its finished looping through every item, calling another function to run. So far i have this:

//Delay function from http://stackoverflow.com/questions/24034544/dispatch-after-gcd-in-swift/24318861#24318861
func delay(delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
}

@IBAction func computerTurn(){
    if(isFirstLevel){levelLabel.text = ("Level 1"); isFirstLevel = false}
    else{ level++ }

    var gameOrderCopy = gameOrder
    var randomNumber = Int(arc4random_uniform(4))
    gameOrder.append(randomNumber)
    var i = 0

    for number in gameOrder{

        if number == 0{
            self.greenButton.highlighted = true
            self.delay(1){
                self.greenButton.highlighted = false
                }
        }

        else if number == 1{
            self.redButton.highlighted = true
            self.delay(1){
                self.redButton.highlighted = false
               }

        }

        else if number == 2{
            self.yellowButton.highlighted = true
            self.delay(1){
                self.yellowButton.highlighted = false
                }
        }
      else if number == 3{
        self.blueButton.highlighted = true
        self.delay(1){
            self.blueButton.highlighted = false
            }
        }
    }
}

But at the moment, all it does is delay one second, then lighting up all the colors at once. What i need is to light up the color in sequential order, with a second delay between each. Thanks in advance!

JohnnyCash
  • 67
  • 1
  • 5

2 Answers2

3

Don't do delay 1, put var delayTime = 1 above the loop, and in the loop do self.delay(delayTime++)

Stripes
  • 4,161
  • 2
  • 25
  • 29
  • Thanks! I see what this code is doing, but do you know how I could make them light up one after each other, because now all light up at once still, but go out at different intervals? – JohnnyCash Mar 29 '15 at 01:37
  • Ah! Wrap the highlighted = true in a delay as well. Depending on the effect you want you may want a second delayTime type variable...depending on the effect you are looking for. – Stripes Mar 29 '15 at 01:45
  • THANKS! This has taken me so long to figure out! Thanks for you help! – JohnnyCash Mar 29 '15 at 01:54
  • 1
    I think your mental model of "delay" is a little different from what it does. Your program would have worked more like you intended if delay actually delayed the given time, did its job and then let your program continue. What it actually does is take the work you give it scribble it down with a note to do it at the appointed time and return promptly and does the next thing in your program. Does that help? – Stripes Mar 29 '15 at 02:05
1

You could try one of two things:

One: Insert sleep(1) at the beginning of your iteration of gameOrder.

Two: Modify your delayfunction a bit

func delay(delay:UInt32, closure:()->()) {
        sleep(delay)
        closure()
    }
Mellson
  • 2,918
  • 1
  • 21
  • 23