Reputation: 53
My code for application will Enter Foreground is given below. I am working on the iOS simulator. What I am trying in my code is, when a player clicks home button while game is going on and returns back to the game after sometime, I want the game to be in pause state. Although my code pauses the game, but it does not pause it immediately. That is, when I go to my game again, there is 1 second of movement before everything pauses.
-(void)applicationWillEnterForeground:(UIApplication *)application
{ [[NSNotificationCenter defaultCenter] postNotificationName:@"didEnterForeground" object:self];
}
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(handleEnterFg)
name: @"didEnterForeground"
object: nil];
-(void) handleEnterFg
{
if (gameIsOver== NO)
{
[myTimer pause];
gamePause = YES;
self.scene.view.paused = YES;
}
}
-(void) handleEnterBg
{
if (gameIsOver== NO)
{
[bgPlayer pause];
[self childNodeWithName:@"pauseButton"].zPosition = -10;
[self childNodeWithName:@"playButton"].zPosition = 160;
}
}
Thank you!
Upvotes: 0
Views: 137
Reputation: 64477
You need to pause the game when you receive applicationWillEnterBackground
if you want to pause it immediately.
Upvotes: 1