如何让 SKAction.sequence 等待随机持续时间
How to get an SKAction.sequence to wait for a random duration
我正在尝试打开一扇门,等待一段随机时间(在一定范围内),然后关闭。如果我使用 SKAction.waitForDuration,我可以设置确切的等待时间,这样就可以了。但是,如果我使用 SKAction.waitForDuration (withRange),它总是在范围内的最短时间准确打开。如何让它在范围内的其他时间打开?任何帮助将不胜感激!谢谢!
这是我的代码:
var doorAction = SKAction.moveTo(CGPoint(x: size.width * 0.5, y: size.height + size.height * 1.95), duration: NSTimeInterval(1))
// randomWait should give me a value between 10 & 30, but door always opens at 10
var randomWait = SKAction.waitForDuration(20.0, withRange: 20.0)
// waitAction works fine, but I want a value between 10 and 30
// var waitAction = SKAction.waitForDuration(10)
var doorReturnAction = SKAction.moveTo(CGPoint(x: size.width * 0.5, y: size.height * 2.18), duration: NSTimeInterval(1))
var actionSequence = SKAction.sequence([doorAction, randomWait, doorReturnAction])
self.runAction(actionSequence)
来自文档。
sec - The average amount of time to wait.
durationRange - The range of possible values for the duration.
如果您将 20 设置为平均值,并且范围仅上升到 20,那么唯一可以满足的方法就是始终以 20 开盘。您应该尝试将第一个参数(平均值)设置为某个值如果您希望行为不太统一,则接近范围的中间。
由于您没有重复动作序列,因此您并不真正需要 SKAction.waitForDuration:withRange:
。您可以使用 arc4random()
计算 waitDuration
let upperlimit : UInt32 = 30
let lowerlimit : UInt32 = 10
let waitDuration = NSTimeInterval(CGFloat(arc4random() % ((upperlimit - lowerlimit) * 10) + lowerlimit * 10)/10.0)
var randomWait = SKAction.waitForDuration(waitDuration)
我正在尝试打开一扇门,等待一段随机时间(在一定范围内),然后关闭。如果我使用 SKAction.waitForDuration,我可以设置确切的等待时间,这样就可以了。但是,如果我使用 SKAction.waitForDuration (withRange),它总是在范围内的最短时间准确打开。如何让它在范围内的其他时间打开?任何帮助将不胜感激!谢谢!
这是我的代码:
var doorAction = SKAction.moveTo(CGPoint(x: size.width * 0.5, y: size.height + size.height * 1.95), duration: NSTimeInterval(1))
// randomWait should give me a value between 10 & 30, but door always opens at 10
var randomWait = SKAction.waitForDuration(20.0, withRange: 20.0)
// waitAction works fine, but I want a value between 10 and 30
// var waitAction = SKAction.waitForDuration(10)
var doorReturnAction = SKAction.moveTo(CGPoint(x: size.width * 0.5, y: size.height * 2.18), duration: NSTimeInterval(1))
var actionSequence = SKAction.sequence([doorAction, randomWait, doorReturnAction])
self.runAction(actionSequence)
来自文档。
sec - The average amount of time to wait.
durationRange - The range of possible values for the duration.
如果您将 20 设置为平均值,并且范围仅上升到 20,那么唯一可以满足的方法就是始终以 20 开盘。您应该尝试将第一个参数(平均值)设置为某个值如果您希望行为不太统一,则接近范围的中间。
由于您没有重复动作序列,因此您并不真正需要 SKAction.waitForDuration:withRange:
。您可以使用 arc4random()
let upperlimit : UInt32 = 30
let lowerlimit : UInt32 = 10
let waitDuration = NSTimeInterval(CGFloat(arc4random() % ((upperlimit - lowerlimit) * 10) + lowerlimit * 10)/10.0)
var randomWait = SKAction.waitForDuration(waitDuration)