3
votes

I'm having difficulty pausing the game when I leave and switch back to it.

I'm trying to pause the SKSpriteNode called main, which contains all my sprites, when the view returns from the background. In-game, I can touch the pause button and the game pauses, and the resume button and it resumes.

This is my code:

func didBecomeActive() {
    println("didBecomeActive")
    main.paused = true
}

The first time this runs is when the app opens for the first time, and everything is paused as it should be. The second time, is when it returns from the background, and suddenly all the animations (SKActions, particles, etc.) start working.

I've confirmed that the method is running, and I've also tried setting main.paused to false and then true, and even self.paused to true. Nothing works.

I'm completely stumped. Anyone know what the issue is here?

1
func applicationWillResignActive(application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - Leo Dabus
@LeonardoSavioDabus I've tried pausing it in that method as well as a few others. The issue is it unpauses (without any code on my part) when I return from the background. - thebro
did you try self.scene.main.pause = true? - sci3nt15t

1 Answers

2
votes

Setting self.scene.paused = YES should fix this. I have tried it with a game I am developing and it works fine.

Just set self.scene.paused = YES when the game enters the background, then when it return to the foreground, it should stay paused till you resume it, i.e. set self.scene.paused = NO.