jeudi 30 juillet 2015

SpriteKit update, how to best call random functions?

I have three functions within my SpriteKit update loop that I want to call at 3 different random intervals, what I am currently doing is generating random numbers each time round the update loop and checking them to see when each function should be called:

let randomCheer = GKRandomSource.sharedRandom().nextIntWithUpperBound(100)
if randomCheer == 99 { print(“CALL: Cheering … Quite a bit”) }

let randomClap = GKRandomSource.sharedRandom().nextIntWithUpperBound(500)
if randomClap == 99 { print(“CALL: Clapping … Not so often”) }

let randomWave = GKRandomSource.sharedRandom().nextIntWithUpperBound(1000)
if randomWave == 99 { print(“CALL: Waving … Very rarely”) }

I could also do something like this and use only one random number.

let randomInt = GKRandomSource.sharedRandom().nextIntWithUpperBound(100)
switch randomInt {
case 0...9:
    print("Often")
case 10...12:
    print("Sometimes")
case 13:
    print("Almost Never")
default:
    continue
}

Is there a better way of doing this that I am maybe missing?




Aucun commentaire:

Enregistrer un commentaire