我需要运行一个未知量(来自字典数组)的SKAction序列,这些序列是动态创建的,并且在循环中使用延迟随着我循环而增加而偏移。循环的打印输出应按以下顺序出现
show line 5
hiding everything
show line 6
hiding everything
show line 2
hiding everything
获取此功能我使用此代码
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
}
}
如果序列只需要发生一次而不是按预期输出,那么有时我需要序列才能永远重复(直到用户停止操作)。永远重复该操作不起作用,因为它重复每个单独的序列并扰乱顺序。第一个序列每1秒重复一次,第二个序列每2秒重复一次......等等。
有关如何让它永远正常循环的任何建议吗?
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
我还尝试将操作附加到一组SKActions并在最后循环它们但它不起作用
var actions = [SKAction]()
...
actions.append(action)
for action in actions {
self.run(action)
}
答案 0 :(得分:2)
如果您唯一需要关注的是未知数量的序列,您可以执行以下操作:
因此,例如,此代码将创建四个框,并将按顺序应用淡入/淡出(代码中的displayAction / hideAction)到每个框,并将永远重复:
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)))
}
}