Call a function in a Timer

0

I have code that animates a falling motion across the screen by spawning enemies at the top and deleting them at the bottom. This is good however all that is needed is to space out the spawning time of the enemies by calling the function spawnEnemy(). How would I go about doing this? Here is my function that makes them move top to bottom, however a bunch of them spawn at once, I only want one to spawn every 2 seconds.

     func random() -> CGFloat {
          return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
     }

     func random(min: CGFloat, max: CGFloat) -> CGFloat {
          return random() * (max - min) + min
     }


    func spawnEnemy() {

    let enemy = SKSpriteNode(imageNamed: "arrow1")

    enemy.name = "arrow1"



    enemy.position = CGPoint(x: frame.size.width * random(min: -0.5,    max: 0.5), y: frame.size.height * random(min: 0.6, max: 0.7))

    addChild(enemy)

    enemy.run(
        SKAction.moveBy(x: 0.0 , y: -size.height - enemy.size.height,
                        duration: TimeInterval(random(min: 4, max: 4))))


    self.enumerateChildNodes(withName: "arrow1") { (node:SKNode, nil) in
        if node.position.y < -550 || node.position.y > self.size.height + 550 {
            node.removeFromParent()



        }
    }
}
swift
sprite-kit
asked on Stack Overflow Feb 25, 2019 by user10101053 • edited Feb 25, 2019 by user10101053

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0