Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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

like image 336
Troy R Avatar asked Nov 24 '25 13:11

Troy R


1 Answers

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)
}
like image 89
Caleb Avatar answered Nov 27 '25 04:11

Caleb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!