[英]How do I randomise a repeatAction count?
I have a SKAction that repeats forever that releases a random number of objects in a wave but I can't seems to find a way to randomise the count of a SKAction.repeatAction each time it's repeated in the SKAction.repeatActionForver. 我有一个永远重复的SKAction,它会在wave中释放一个随机数量的对象但是我似乎无法在SKAction.repeatActionForver中重复每次重复SKAction.repeatAction的计数。 Anyone know a solution to my issue?
有人知道我的问题的解决方案吗?
let objectSet = SKAction.repeatAction(SKAction.sequence([addObject, objectDelay]), count: random value )) let objectSet = SKAction.repeatAction(SKAction.sequence([addObject,objectDelay]),count: random value ))
let setDelay = SKAction.waitForDuration(2.0, withRange: 1.0)
let objectDelay = SKAction.waitForDuration(0.6, withRange: 0.4)
let objectSet = SKAction.repeatAction(SKAction.sequence([addObject, objectDelay]), count: *Trying to get a random value*))
objectLayerNode.runAction(SKAction.repeatActionForever(SKAction.sequence([objectSet, setDelay])))
Thanks 谢谢
You can use let random = Int(arc4random_uniform(UPPER_BOUND))
. 您可以使用
let random = Int(arc4random_uniform(UPPER_BOUND))
。
Then 然后
let objectSet = SKAction.repeatAction(SKAction.sequence([addObject, objectDelay]), count: random))
You could also do a range with an upper and lower bound. 您还可以执行具有上限和下限的范围。
let random = LOWER_BOUND + arc4random_uniform(UPPER_BOUND - LOWER_BOUND + 1)
EDIT 编辑
You can use recursion. 你可以使用递归。 Reference
参考
let setDelay = SKAction.waitForDuration(2.0, withRange: 1.0)
let objectDelay = SKAction.waitForDuration(0.6, withRange: 0.4)
func repeat() {
let random = LOWER_BOUND + arc4random_uniform(UPPER_BOUND - LOWER_BOUND + 1)
let objectSet = SKAction.repeatAction(SKAction.sequence([addObject, objectDelay]), count: random))
let sequence = SKAction.sequence([
objectSet, objectDelay,SKAction.runBlock({
[unowned self] in self.repeat()
})
])
objectLayerNode.runAction(sequence)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.