简体   繁体   中英

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

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM