Alright so I'm building a sprite kit game where I need to delay an enemy spawn func repeatedly.
I've managed to generate random Double
s using this function:
func randomDelay()->Double {
var randLower : UInt32 = 1
var randUpper : UInt32 = 50
var randDelayTime = arc4random_uniform(randUpper - randLower) + randLower
var randDelayTimer = Double(randDelayTime) / 10
//var randDelay = randDelayTimer * Double(NSEC_PER_SEC)
//var newTime = dispatch_time(DISPATCH_TIME_NOW, Int64(randDelay))
println(randDelayTimer)
return randDelayTimer
}
I know this works because I printed randDelayTimer
out several times and it does in fact generate random Double
s.
Here is where I try to spawn my enemies after the random delay repeatedly:
runAction(SKAction.repeatActionForever(
SKAction.sequence([
SKAction.waitForDuration(NSTimeInterval(randomDelay())),
SKAction.runBlock({self.spawnEnemy1()})))
This works, but only the first time it is run. Whatever random Double
is generated by randomDelay()
the FIRST TIME the sequence is cycled through is applied to every enemy that is spawned after that. For example if when the game is run, randomDelay()
= 3.5, a 3.5 sec delay is repeated forever.
I know that randomDelay()
is only being run that first time also because nothing is printed to the console after the first time.
How can I fix this?