gpt4 book ai didi

swift - 如何循环动态延迟的 SKActions 序列

转载 作者:搜寻专家 更新时间:2023-10-31 22:43:06 26 4
gpt4 key购买 nike

我需要运行未知数量(来自字典数组)的 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
}
}

如果序列只需要发生一次而不是按预期输出,但是有时我需要序列永远重复(直到用户停止操作)。在 Action 周围永远重复是行不通的,因为它会重复每个单独的序列并打乱顺序。第一个序列将每 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

我也试过将 Action 附加到 SKActions 数组并在最后循环遍历它们,但它没有用

var actions = [SKAction]()

...

actions.append(action)

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

最佳答案

如果您唯一关心的是永远重复未知数量的序列,您可以执行以下操作:

  • 创建所有需要的序列
  • 将它们放在另一个序列中
  • 永远循环

因此,例如,此代码将创建四个框,并将淡入/淡出(代码中的 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)))
}
}

关于swift - 如何循环动态延迟的 SKActions 序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43238226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com