2
votes

I have a run action loop that spawns an enemy then waits and spawns another. The intention is that the higher the score the quicker the enemies spawn.

But the current code has the enemies spawn at the same rate no matter what. I update the rate in the override func update(currentTime: NSTimeInterval) method all the time, so I don't know what's wrong.

override func update(currentTime: NSTimeInterval){
spawnRate = 2.0-(0.1*(Double)(((Double)(Score))/(10.0)))
    if(spawnRate < 0.5){
        spawnRate = 0.5
    }
}
override func didMoveToView(view: SKView) {
    runAction(SKAction.repeatActionForever(
            SKAction.sequence([
                SKAction.runBlock(addEnemy),
                SKAction.waitForDuration(spawnRate)
                ])
            ))
}
2
we can't really help you unless you show some code. - Peyman
You need to show what you have tried for others to better understand how to help you. - Devapploper
there's some more code, I've changed the spawn rate during the game and they spawn at the initial rate i set. That's all i've tried. - C_Toll7

2 Answers

1
votes

The problem is that This:

SKAction.sequence([
     SKAction.runBlock(addEnemy),
     SKAction.waitForDuration(spawnRate)
])

is made one's and then just repeated forever.

try to put the code in a

runAction(SKAction.repeatActionForever(runblock(spawnAndWait()))

and then make a seperate func.

spawnAndWait() { // put here the code for spawning the enemy and waiting spawnrate duration.

b.t.w
Put the code that updates the spawnRate where the score is increased. Because that is more efficient. then in the update method because that is constantly called.

1
votes

What is happening currently is that you've:

  • stored duration paramter inside an action
  • reused that action within sequence over and over again

So nothing actually changes. To solve this, one way would be to use recursive call and change duration parameter each time:

import SpriteKit

class GameScene: SKScene {

    let  shape = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 20, height: 20))

    var delay:NSTimeInterval = 1.0

    override func didMoveToView(view: SKView) {

       recursiveMethod()

    }

    func recursiveMethod(){



        let recursive = SKAction.sequence([

            SKAction.waitForDuration(delay),
            SKAction.runBlock({self.recursiveMethod();println("delay \(self.delay)")})
         ])

        runAction(recursive)

    }

    override func update(currentTime: NSTimeInterval) {
        if(delay > 0.2){delay = delay - 0.001}
    }

}