Problem: I have a game which generates wall in every 2 seconds. When I press Home button, and then come back to my game, the pattern of wall creation is disrupted. My theory is that when the game is suspended via Home button, the game scene stops but the timer doesnt stop and continues to create the walls which causes the problem.
I tried to stop the timer of wall creation when the game is suspended:
WallGenerationTimer = NSTimer.scheduledTimerWithTimeInterval(seconds, target: self , selector: "generateWall", userInfo: nil, repeats: true)
I checked several answers but all of them explains the solution when NSTimer sits in GameViewController and I cant access my NSTimer in GameScene from GameViewController. The answers are based on using to send notification from applicationWillResignActive and applicationDidEnterBackground functions in AppDelegate.swift file, then using an observer in GameViewController to stop the timer in it.
However, my timer is in GameScene and I couldnt figure out how to stop it when the game is suspended.
What I tried:
I tried to add an observer in GameScene inside the update function and I call a function when the notification is sent through applicationWillResignActive and applicationDidEnterBackground, this function stops timer by:
generationTimer?.invalidate()
this didnt work and the problem still exists.