以随机时间间隔定期将节点添加到场景中
Periodically add nodes to the scene with a random time interval in between
简化一下,我正在尝试弄清楚如何在一段时间内向我的场景添加假设 10 个节点,每个节点由一些随机时间间隔分隔。
例如:
Loop 10 times:
A. Create and add node to scene
B. Wait some random amount of time, 1-5 seconds
C. Back to A
最好也知道何时完成 运行,让布尔值在最后一个节点添加到场景时翻转,但这需要知道最后一个节点何时完成添加。我不太确定如何完成此操作。我阅读了一些有关 dispatch_after
的内容,但这并不能解决我在添加节点之间的随机时间问题。
在 SpriteKit 中,您通常使用 SKAction
及其 waitForDuration(withRange:) 方法来做到这一点。重要的部分是 withRange
参数(引用自文档):
Each time the action is executed, the action computes a new random
value for the duration. The duration may vary in either direction by
up to half of the value of the durationRange parameter.
例如,如果您的等待持续时间为 3 秒,范围参数设置为 2,您将获得 2 到 4 秒的延迟。
所以你可以这样做:
class GameScene: SKScene, SKPhysicsContactDelegate {
var lastSpawnTime:Date?
override func didMove(to view: SKView) {
let wait = SKAction.wait(forDuration: 3, withRange: 4)
let block = SKAction.run {[unowned self] in
//Debug
let now = Date()
if let lastSpawnTime = self.lastSpawnTime {
let elapsed = now.timeIntervalSince(lastSpawnTime)
print("Sprite spawned after : \(elapsed)")
}
self.lastSpawnTime = now
//End Debug
let sprite = SKSpriteNode(color: .purple, size: CGSize(width: 50, height: 50))
self.addChild(sprite)
}
let sequence = SKAction.sequence([block, wait])
let loop = SKAction.repeat(sequence, count: 10)
run(loop, withKey: "aKey")
}
}
你会在控制台中看到如下内容:
Spawning after : 1.0426310300827
Spawning after : 1.51278495788574
Spawning after : 3.98082602024078
Spawning after : 2.83276098966599
Spawning after : 3.16581499576569
Spawning after : 1.84182900190353
Spawning after : 1.21904700994492
Spawning after : 3.69742399454117
Spawning after : 3.72463399171829
简化一下,我正在尝试弄清楚如何在一段时间内向我的场景添加假设 10 个节点,每个节点由一些随机时间间隔分隔。
例如:
Loop 10 times:
A. Create and add node to scene
B. Wait some random amount of time, 1-5 seconds
C. Back to A
最好也知道何时完成 运行,让布尔值在最后一个节点添加到场景时翻转,但这需要知道最后一个节点何时完成添加。我不太确定如何完成此操作。我阅读了一些有关 dispatch_after
的内容,但这并不能解决我在添加节点之间的随机时间问题。
在 SpriteKit 中,您通常使用 SKAction
及其 waitForDuration(withRange:) 方法来做到这一点。重要的部分是 withRange
参数(引用自文档):
Each time the action is executed, the action computes a new random value for the duration. The duration may vary in either direction by up to half of the value of the durationRange parameter.
例如,如果您的等待持续时间为 3 秒,范围参数设置为 2,您将获得 2 到 4 秒的延迟。
所以你可以这样做:
class GameScene: SKScene, SKPhysicsContactDelegate {
var lastSpawnTime:Date?
override func didMove(to view: SKView) {
let wait = SKAction.wait(forDuration: 3, withRange: 4)
let block = SKAction.run {[unowned self] in
//Debug
let now = Date()
if let lastSpawnTime = self.lastSpawnTime {
let elapsed = now.timeIntervalSince(lastSpawnTime)
print("Sprite spawned after : \(elapsed)")
}
self.lastSpawnTime = now
//End Debug
let sprite = SKSpriteNode(color: .purple, size: CGSize(width: 50, height: 50))
self.addChild(sprite)
}
let sequence = SKAction.sequence([block, wait])
let loop = SKAction.repeat(sequence, count: 10)
run(loop, withKey: "aKey")
}
}
你会在控制台中看到如下内容:
Spawning after : 1.0426310300827
Spawning after : 1.51278495788574
Spawning after : 3.98082602024078
Spawning after : 2.83276098966599
Spawning after : 3.16581499576569
Spawning after : 1.84182900190353
Spawning after : 1.21904700994492
Spawning after : 3.69742399454117
Spawning after : 3.72463399171829