我目前有下面的代码,每 2 秒愉快地重复一个 Action ,但我希望它发生在一个完全随机的时间段,例如 1 到 10 秒之间的某个时间,我使用 arch4random 尝试了几行不同的代码其中没有工作。我希望这是一个简单的修复,非常感谢任何建议。
感谢 runAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.runBlock(callBird), SKAction.waitForDuration(2.0)])))
最佳答案
如果你试图随机化一个持续时间参数,一个 + waitForDuration:withRange:将完成这项工作。
runAction(
SKAction.repeatActionForever(
SKAction.sequence([SKAction.runBlock({/*do your stuff*/}),
SKAction.waitForDuration(2, withRange: 3) ])))
来自文档:
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.
意味着如果将持续时间设置为 2 并将范围参数设置为 3,则可能的持续时间值将在 0.5 和 3.5 之间变化。
关于swift - 如何让 SKAction 在随机时间后重复自身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33927017/