-2

I'm looking to integrate some delayed functions into my game, currently I'm working on putting a 5 second delay on calling my first function as in this time period the game goes through a countdown to get the player ready.

Currently my countdown is displayed but the game is in full swing in the background. I've got one function I'd like to delay for 5 seconds but I'm also thinking of using the same method to incorporate other objects later in the game to boost difficulty.

The call I'm looking to delay is moving.addChild(crows)

If anyone can show me how to build that would be great as I've been unable to find on site (unless I've missed it).

Rich Townsend
  • 571
  • 1
  • 5
  • 21

2 Answers2

3

Don't use NSTimer in SpriteKit. Use this instead:

let delay = SKAction.waitForDuration(5.0)
someNode.runAction(delay) {
     //run code here after 5 secs
}
dennism
  • 513
  • 5
  • 16
0
let delay = 5.0 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue()) {
    moving.addChild(crows)
}
Brian
  • 30,156
  • 15
  • 86
  • 87
  • Thank you for this, it works in principal however there is a slight niggle, my game generates a crow every second and throws it across the screen, using the delay the game appears to save up the crows and then throw them all in one go after the five seconds. Would you know of a way around this? – Rich Townsend Aug 16 '15 at 07:08
  • You meant you need to throw a crow across the screen every 1 second? I didn't get you. – Brian Aug 16 '15 at 07:33
  • My code throws a crow across the screen every second but I want to delay that from being called by 5 seconds. I want the crows generated every second but not when the game first loads. – Rich Townsend Aug 16 '15 at 07:48