How to encode a dynamically delayed SKActions sequence
I need to run an unknown sum (from an array of dictionaries) of SKAction sequences that are dynamically created and offset in a loop using a delay that increases as the loop goes through. the printed result of the loop should appear in the following order
show line 5
hiding everything
show line 6
hiding everything
show line 2
hiding everything
to get this functionality i use this code
func display(winningLines: [Int : AnyObject]) {
var delay = 0.0
var actions = [SKAction]()
let hideAction = SKAction.run {
print("hiding everything")
}
for 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 the sequence only needs to happen once than it is outputted as expected, however there are times when I need the sequence to just keep repeating forever (until the user terminates the action). Imposing a repetition forever around an action doesn't work because it repeats every single sequence and breaks the order. The first sequence will repeat every 1 second, the second sequence will repeat every 2 seconds ... etc.
Are there any suggestions on how to do this correctly for a loop?
line 5
hiding everything
line 5
line 6
hiding everything
line 5
hiding everything
line 2
hiding everything
line 5
line 6
hiding everything
hiding everything
line 5
hiding everything
hiding everything
line 5
line 6
line 2
hiding everything
line 5
hiding everything
hiding everything
I also tried adding actions to the SKActions array and looping through to the end, but that didn't work
var actions = [SKAction]()
...
actions.append(action)
for action in actions {
self.run(action)
}
source to share
If your only problem is repeating forever, an unknown number of sequences, you can do the following:
- create all the necessary sequences
- put them in a different sequence
- a cycle that is forever
So, for example, this code will create four windows and will consistently apply fade in / out (displayAction / hideAction from your code) to each window and repeat this forever:
import SpriteKit
class GameScene: SKScene {
var boxes:[SKSpriteNode] = []
override func didMove(to view: SKView) {
let distance:CGFloat = 10
for i 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>, with event: UIEvent?) {
var actions:[SKAction] = []
let action = SKAction.sequence([
SKAction.fadeIn(withDuration: TimeInterval(1)),
SKAction.fadeOut(withDuration: 0),
])
for 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)))
}
}
source to share