0

I would like to change from scene to scene after a certain amount of time on one scene in Swift. I am trying to create a survival type game that the player has to survive a level for so long before they can advance to the next level.

I know that after I get to func being called I will be using this to go to the next scene.

self.view?.presentScene(GameScene2())

I am sure that something along the lines of NSTimer is going to be used, but anything that can be given to point me in the right direction would be of great help. Thank you in advance.

Lozo
  • 79
  • 10
  • 1
    Before asking questions it is best to read any documentation you can find, try to do it yourself, and then post your failures along with your question so people may see where your mistakes are and point them out to you. – MikeG Feb 01 '16 at 04:40
  • 1
    I will be sure to do that. I did attempt to do this, but I was not able to find the answer by this method – Lozo Feb 01 '16 at 04:41

2 Answers2

2

An NSTimer is one option. Depending on what sort of accuracy you need and how long your duration will be you may or may not want to use an NSTimer. Example of an NSTimer ...

var myTimer = NSTimer()

func startMyTimer() {
myTimer = NSTimer.scheduledTimerWithTimeInterval(1.5, target: self, selector: "myFunction", userInfo: nil, repeats: true)
//1.5 is the time interval that you want to call the timer, in this case every 1.5 seconds
//the selector calls myFunction which is the function that you want to call every time the timer reaches its time interval
//if you want the timer to repeat and call myFunction() every 1.5 seconds then repeat should be true. if you only want it to be called once then repeat should be false
}

func myFunction(){
    //do whatever i am supposed to do when the timer reaches my specified interval
}

Now this may not be solution you are looking for. Another way is to use GCD's (grand Central Dispatch) dispatch_after . A very nice and extremely easy to use function can be found here, dispatch_after - GCD in swift? , in the second answer down, compliments of stackoverflow's @matt

Community
  • 1
  • 1
MikeG
  • 3,745
  • 1
  • 29
  • 51
  • I am sure that this would have worked great, but the answer listed prior to this was efficient. – Lozo Feb 01 '16 at 04:41
  • 1
    Understandable, thank you for explanation. I am happy to help. be sure to call `myTimer.invalidate()` when you want to stop the timer. – MikeG Feb 01 '16 at 04:42
  • @MikeG In SpriteKit, a preferred way for time related actions would be SKAction or update: method and its currentTime parameter. NSTimer doesn't respect scene's or view's paused state, so at least, you should implement a pause feature. Otherwise, imagine what will happen if NSTimer is not paused and user pauses the game, or receives a phone call... Read more here about [NSTimer and GCD](http://stackoverflow.com/a/23978854/3402095) vs SKAction and update: – Whirlwind Feb 01 '16 at 11:47
  • @Whirlwind That makes sense. Question asker specified NSTimer in their question so I gave that example. I thought the most accurate timer and the most precise timer was a CADisplayLink, which is updated every 1/60 second and thus in sync with the UI frame refresh rate... which would make it the best options for games – MikeG Feb 01 '16 at 14:04
0

When your view is instantiated, you want to begin an NSTimer. See this page for help creating this: How can I use NSTimer in Swift?. This page may help as well: Using an NSTimer in Swift.

override func viewDidLoad() {
super.viewDidLoad()

var timer = NSTimer.scheduledTimerWithTimeInterval(30.0, target: self, selector: "update", userInfo: nil, repeats: true)

Then, you can just put the

self.view?.presentScene(GameScene2())

in a func update like so:

func update() {

self.view?.presentScene(GameScene2())

Hope this helps. If so, please check off my answer. If not, comment and I will try and help again.

Community
  • 1
  • 1
jbcd13
  • 145
  • 8
  • This works perfectly. Thank you. I will simply be able to reuse this for each level of my game that I create I appreciate it! Now i am running into a problem that I need to update the score from scene to scene. I am not sure how to keep updating the score from Level 1 as it increases on Level 2. – Lozo Feb 01 '16 at 04:40
  • Read here about [possible issues](http://stackoverflow.com/a/23978854/3402095) with NSTimer when used with SpriteKit. – Whirlwind Feb 01 '16 at 11:48