如何随机化 repeatAction 计数?
How do I randomise a repeatAction count?
我有一个永远重复的 SKAction,它会在波浪中释放随机数量的对象,但我似乎无法找到一种方法来随机化 SKAction.repeatAction 每次在 SKAction.repeatAction永远。有人知道我的问题的解决方案吗?
let objectSet = SKAction.repeatAction(SKAction.sequence([addObject, objectDelay]), count: 随机值))
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])))
谢谢
您可以使用 let random = Int(arc4random_uniform(UPPER_BOUND))
.
然后
let objectSet = SKAction.repeatAction(SKAction.sequence([addObject, objectDelay]), count: random))
你也可以做一个有上限和下限的范围。
let random = LOWER_BOUND + arc4random_uniform(UPPER_BOUND - LOWER_BOUND + 1)
编辑
你可以使用递归。
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)
}
我有一个永远重复的 SKAction,它会在波浪中释放随机数量的对象,但我似乎无法找到一种方法来随机化 SKAction.repeatAction 每次在 SKAction.repeatAction永远。有人知道我的问题的解决方案吗?
let objectSet = SKAction.repeatAction(SKAction.sequence([addObject, objectDelay]), count: 随机值))
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])))
谢谢
您可以使用 let random = Int(arc4random_uniform(UPPER_BOUND))
.
然后
let objectSet = SKAction.repeatAction(SKAction.sequence([addObject, objectDelay]), count: random))
你也可以做一个有上限和下限的范围。
let random = LOWER_BOUND + arc4random_uniform(UPPER_BOUND - LOWER_BOUND + 1)
编辑
你可以使用递归。
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)
}