I have my game in iphone. It restart when i attend a call. How to make it resume instead of restart the game
How to resume my game after attending a call in iphone
(8 posts) (6 voices)-
Posted 1 year ago #
-
This
- (void)applicationDidBecomeActive:(UIApplication *)application;In the app delegate, this is ran when the application regains focus after a phonecall/text message/notification
if you followed the templates it should already be implemented as
- (void)applicationDidBecomeActive:(UIApplication *)application { [[CCDirector sharedDirector] resume]; }Posted 1 year ago # -
i have the same question, and i think when the game quited since the phone call, we should save the game status firstly in the delegate "applicationWillResignActive"(is the function name correct? maybe i spell it wrong), shouldn't we?
Posted 1 year ago # -
I used these two functions.
- (void)applicationWillResignActive:(UIApplication *)application {
[[CCDirector sharedDirector] pause];
}- (void)applicationDidBecomeActive:(UIApplication *)application {
[[CCDirector sharedDirector] resume];
}But still my game restarts when i ended a incoming call. Is there any other function to resume
Posted 1 year ago # -
^ that is normal behaviour on iOS 3.x. If the user takes the call your app is terminated, you have to write code to remember what you were doing and then resume when the app is restarted. If the user rejects the call then didBecomeActive is called.
Posted 1 year ago # -
do you mean record the current status in the file or database when exiting and read it out when resuming?
Posted 1 year ago # -
I had a lot of help from others on this forum, such as Codemattic, on this. I used an example someone else wrote for "GlobalDataManager" that I found for this. If I could remember where I got the code I'd credit them, but I forgot. If someone knows who wrote the GlobalDataManager example, please post.
What I have in my game is something as below. I've cut it down to just show how to store and recall 3 variables as an example.
First, in the appdelegate.m file, you need to store your runtime game data you want to save before the app shuts down. I do this in the following sections:
- (void)applicationWillResignActive:(UIApplication *)application { [[GlobalDataManager sharedDataManager] StoreData]; // save the game values to a file before shutting down [[CCDirector sharedDirector] pause]; }and
- (void)applicationWillTerminate:(UIApplication *)application { [[GlobalDataManager sharedDataManager] StoreData]; // save the game values to a file before shutting down // above added to applicationWillResignActive so that it will store data even in new multitasking UI [[CCDirector sharedDirector] end]; }In the appdelegate.m file, you need to recall your runtime game data before the app starts back up. I do this in the following section:
- (void) applicationDidFinishLaunching:(UIApplication*)application { [[GlobalDataManager sharedDataManager] RecallData]; // recalls the game values from a file }So when the app is starting up , or shutting down, the appdelegate.m file calls the methods in GlobalDataManager to store and recall the data you want.
In my GlobalDataManager.m file I have "StoreData" and "RecallData" methods. These are used to actually store and recall the game data I want in files so the player can start the game and resume where they left off.
In the GlobalDataManager.h and GlobalDataManager.m files , below is an example of storing and recalling 3 game variables, which should get you started. I cut out a lot of the other variables I save to make the code readable, so hopefully I didn't leave something out.
In GlobalDataManager.h :
// GlobalDataManager.h #import <Foundation/Foundation.h> //This is a singleton, responsible for storing data that is accessible anywhere in the game @interface GlobalDataManager : NSObject { float currentScore; // score total for the current game float levelScore; // score total for the current level int currentLevel; // current game level } @property (nonatomic, readwrite) float currentScore; @property (nonatomic, readwrite) float levelScore; @property (nonatomic, readwrite) int currentLevel; + (GlobalDataManager*)sharedDataManager; -(void) StoreData; // saves the game data from a file in NSUserDefaults directory -(void) RecallData; // gets the game data from a file in NSUserDefaults directory @endIn GlobalDataManager.m :
// GlobalDataManager.m #import "GlobalDataManager.h" static GlobalDataManager *sharedGlobalDataManager = nil; @implementation GlobalDataManager @synthesize currentScore; @synthesize levelScore; @synthesize currentLevel; + (GlobalDataManager*)sharedDataManager { @synchronized(self) { if (sharedGlobalDataManager == nil) { [[self alloc] init]; // assignment not done here } } return sharedGlobalDataManager; } + (id)allocWithZone:(NSZone *)zone { @synchronized(self) { if (sharedGlobalDataManager == nil) { //NSLog(@"Global Data Manager: Creating Singleton"); sharedGlobalDataManager = [super allocWithZone:zone]; //Do variable initialize stuff here, if you need return sharedGlobalDataManager; // assignment and return on first allocation } } return nil; //on subsequent allocation attempts return nil } -(void)StoreData // save the game data in a file in NSUserDefaults directory { // called automatically from iHaggleAppDelegate - applicationWillTerminate and/or applicationWillResignActive //NSLog(@"Global Data Manager: Storing Game Data Start"); [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:currentScore] forKey:@"currentScore"]; [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:levelScore] forKey:@"levelScore"]; [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:currentLevel] forKey:@"currentLevel"]; return; } -(void)RecallData // get the game data from a file in NSUserDefaults directory { // called automatically from iHaggleAppDelegate - applicationDidFinishLaunching //NSLog(@"Global Data Manager: Recalling Game Data Start"); currentScore = [[NSUserDefaults standardUserDefaults] floatForKey:@"currentScore"]; levelScore = [[NSUserDefaults standardUserDefaults] floatForKey:@"levelScore"]; currentLevel = [[NSUserDefaults standardUserDefaults] integerForKey:@"currentLevel"]; //NSLog(@"Global Data Manager: Recalling Game Data Done"); return; } - (id)copyWithZone:(NSZone *)zone { return self; } - (id)retain { return self; } - (unsigned)retainCount { return UINT_MAX; //denotes an object that cannot be released } - (void)release { //do nothing } - (id)autorelease { return self; } @endPosted 1 year ago # -
Also to remember, and I forget who initially noticed this, but on iPhone4, if someone suspends your app and then kills it from the multitask menu, it won't receive a call to appWillTerminate so you should also save stuff in appWillResignActive.
Posted 1 year ago #
Reply
You must log in to post.