swift call function multiple times inside update

Viewed 1622

I create a game via SpriteKit. in the game every few second a ball spawn to the screen (via function) and the player has to blow them up. now, I want to check the player level via his score so if the score is bigger than 10 the spawnBall function will be executed twice (so 2 ball will spawn on the screen) an so on. I tried to to it via the update fun (that will "read" the player score and depends on the score will call the spawnBall function). Unfortunately when I do it the screen is spawn with million (or so) balls in few seconds (as I said I want it to call the function every few seconds and increase the call while the score is X). I really don't have any idea how to do it. here is my code:

override func update(_ currentTime: CFTimeInterval) {

    if (self.score <= 10){
        spawnBalls()
    }

    if (self.score > 10 && self.score <= 20){
        spawnBalls()
        spawnBalls()
    }

    if (self.score > 20){
        spawnBalls()
        spawnBalls()
        spawnBalls()
    }

    if (self.subscore == 3) {
        _ = randomBallColorToBlow()
        self.subscore = 0
    }
}

func spawnBalls() {
    let wait = SKAction.wait(forDuration: 1)
    let action = SKAction.run {
        self.createBall()
    }
    run(SKAction.repeatForever((SKAction.sequence([wait, action]))))
}

how can I do it without using the update function??

2 Answers
Related