swift - How to loop a dynamically delayed sequence of SKActions -


i need run unknown amount (from array of dictionaries) of skaction sequences dynamically created , offset in loop using delay increases go through loop. printed output of loop should appear in following order

show line 5 hiding show line 6 hiding show line 2 hiding 

to obtain functionality use code

func display(winninglines: [int : anyobject]) {      var delay = 0.0     var actions = [skaction]()      let hideaction = skaction.run {         print("hiding everything")     }      winningline in winninglines {          let displayaction = skaction.run {             print("line \(winningline.key)")         }          let wait = skaction.wait(forduration: delay)                     var action: skaction!          //repeatforever = true         if !repeatforever {              action = skaction.sequence([wait, displayaction, skaction.wait(forduration: 1.0), hideaction])         }         else {             let seq = skaction.sequence([wait, displayaction, skaction.wait(forduration: 1.0), hideaction])             action = skaction.repeatforever(seq)         }         self.run(action)          delay += 1.0     } } 

if sequence needs occur once outputs expected, there times need sequence keep repeating forever (until user stops action). putting repeat forever around action doesn't work because repeats each individual sequence , messes order. first sequence repeat every 1 second, second sequence repeat every 2 seconds...etc.

any suggestions on how loop forever?

line 5 hiding line 5 line 6 hiding line 5 hiding line 2 hiding line 5 line 6 hiding hiding line 5 hiding hiding line 5 line 6 line 2 hiding line 5 hiding hiding 

i've tried appending actions array of skactions , looping through them @ end didn't work

var actions = [skaction]() 

...

actions.append(action)  action in actions {     self.run(action) } 

if concern repeat forever, unknown amount of sequences, can following:

  • create required sequences
  • put them in sequence
  • loop forever

so example, code create 4 boxes, , apply fade in/out (displayaction/hideaction code) sequentially every box, , repeat forever:

import spritekit  class gamescene: skscene {  var boxes:[skspritenode] = []  override func didmove(to view: skview) {      let distance:cgfloat = 10      in 0...3 {          let box = skspritenode(color: .purple, size: cgsize(width: 50, height: 50))         box.name = string(i) //name == index         box.position.x = cgfloat(i) * (box.size.width + distance)         box.alpha = 0.0         addchild(box)         boxes.append(box)      } }  override func touchesbegan(_ touches: set<uitouch>, event: uievent?) {      var actions:[skaction] = []      let action = skaction.sequence([          skaction.fadein(withduration: timeinterval(1)),         skaction.fadeout(withduration: 0),     ])      index in 0...boxes.count {          let sequence = skaction.sequence([             skaction.run(action, onchildwithname: string(index)),             skaction.wait(forduration: 1)         ])          actions.append(sequence)      }      self.run(skaction.repeatforever(skaction.sequence(actions))) } } 

Comments