Reputation: 140
I am trying to make it so that my game pauses when a battery warning pops up, or there is an incoming call. I know the method "applicationWillResignActive:" is called for one of these interruptions but here is my problem. My current Pause method is in my custom Game class and I'm not sure how to call it from the app delegate or if there is something else I should do. Thanks!
Upvotes: 2
Views: 434
Reputation: 3974
Set up a notification using NSNotificationCenter
. When applicationWillResignActive:
is called fire off a notification. Your custom game class should then be set up as an observer of the notification and pause the game when the notification comes in. Here's info on using NSNotificationCenter
:
Here is a stack overflow question that also shows how to use it: How to use NSNotification
There are also many many tutorials on-line that explain how to use it.
Upvotes: 1
Reputation: 5388
Use the Notification Center to send a message to another part of your code. When you receive the applicationWillResignActive:
call, you post a message to Notification center:
[[NSNotificationCenter defaultCenter] postNotificationName:@"pauseGame" object:self];
Then, you set your game code to listen for these notifications (when the game is initialized):
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receivePauseNotification:)
name:@"pauseGame"
object:nil];
Your method that will be called from this just needs to pause the game, however it is supposed to do that according to your own code:
- (void) receivePauseNotification:(NSNotification *) notification
{
if (![[notification name] isEqualToString:@"pauseGame"])
{
NSLog (@"Error; unknown notification received");
return;
}
// pause the game here.
}
Upvotes: 2