3

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. Anyone know a solution to my issue?

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

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Troy R
  • 426
  • 2
  • 16

1 Answers1

3

You can use 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)
}
Community
  • 1
  • 1
Caleb
  • 5,548
  • 3
  • 25
  • 32
  • I tried adding a random let with a upper and lower bound but it seems to only randomise the count once and keeps the same value for each repeatForever loop of the repeatAction. – Troy R Dec 20 '15 at 04:43
  • It seems to still only get a random count at the start and keep that value. Eg: repeatAction count gives 5, 5, 5, 5, 5... in each repeat not a random value like 5, 7, 10, 14, 4... – Troy R Dec 20 '15 at 05:06
  • Works perfectly. Thanks Caleb – Troy R Dec 20 '15 at 05:29