如何循环动态延迟的 SKActions 序列
How to loop a dynamically delayed sequence of SKActions
我需要 运行 未知数量(来自字典数组)的 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)
}
如果您唯一关心的是永远重复未知数量的序列,您可以执行以下操作:
- 创建所有需要的序列
- 将它们放在另一个序列中
- 永远循环
因此,例如,此代码将创建四个框,并将淡入淡出 in/out(来自您的代码的 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)))
}
}
我需要 运行 未知数量(来自字典数组)的 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)
}
如果您唯一关心的是永远重复未知数量的序列,您可以执行以下操作:
- 创建所有需要的序列
- 将它们放在另一个序列中
- 永远循环
因此,例如,此代码将创建四个框,并将淡入淡出 in/out(来自您的代码的 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)))
}
}