jeudi 9 juillet 2015

Swift: random delay generation not working?

Ok, so I'm creating a sprite kit game and what I'm trying to do is generate a random Double and then feed it into an SKAction.waitForDuration so that a random delay is executed.

I'm using this delay to delay the spawning of my enemies within the game. Here is how I'm trying to generate and run a random delay:

func randomDelay() {
        var randLower : UInt32 = 1
        var randUpper : UInt32 = 50
        var randDelayTime = arc4random_uniform(randUpper - randLower) + randLower
        var randDelayTimer = Double(randDelayTime) / 10

        runAction(SKAction.waitForDuration(randDelayTimer))
        println(randDelayTimer)
    } 

Here I execute this function prior to spawning my enemy:

runAction(SKAction.repeatActionForever(
            SKAction.sequence([
                SKAction.runBlock({self.randomDelay()}),
                SKAction.runBlock({self.spawnEnemy1()})
)))

And the code doesn't crash but no delay is being executed. The enemies are just spawned over and over at the same rate.

I know that the randomDelay() function is producing a random Double because when I println(randDelayTimer), I get a random Double every time like I should.

Why isn't the delay in randomDelay() being run? How can I fix this?




Aucun commentaire:

Enregistrer un commentaire