How to remain pause after resume the game from background in cocos2d? - iphone

I have a toggle menu which toggle between the words "Pause" and "Resume" when it is pressed, which also pause and resume the whole game. This means when playing, the menu will be shown as "Pause" (tap here to pause), when pausing the menu will be shown as "Resume" (tap here to resume).
Here is the problem, if I tap the home button after I pause the game, then go back into it, it resumes itself and the pause menu is shown as "Resume". And this doesn't make sense to me. The best way I want is to pause the game whenever go into the background and resume from background. I look at the following methods, but they don't really work:
-(void)applicationWillResignActive:(UIApplication *)application{
}
- (void)applicationDidBecomeActive:(UIApplication *)application{
}
-(void)applicationWillEnterBackground:(UIApplication*)application{
}
-(void)applicationDidEnterBackground:(UIApplication *)application {
}
I even just put CCLOG in all of those methods, but nothing has been called. Is there something I am need to put/declare before I use those methods?
Sorry, it is a bit too long to read. Hope you can help me. Thank you.

I'm not sure why, but for my experience (not cocos2d), there's no additional implementation if want apply those methods.
perhaps, you should try look at this.
link 1 & link 2

-(void)applicationWillResignActive:(UIApplication *)application{
}
- (void)applicationDidBecomeActive:(UIApplication *)application{
}
you need custom your cocos2d CCDIRECTOR class. And get now display layer. then active it pause or resume function. All this must need some protocol.

These methods are invoked when the iOS forces your application in the background, or resumes execution of your application, ie they are signals you receive when an external event causes your application to go to background, or come back from it. You should not try to invoke them directly. There is no real relationship with a 'user created' menu like yours (like your Resume/Pause menu), unless you make the relationship explicit.
So , in the following method:
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(#"<%#>:applicationWillResignActive - received signal, pausing sharedDirector.",[self class]);
// here : place your code for forcing your menu in the 'Resume' state
// i am assuming some kind of change in a button, and
// a state variable of your own that define and control
// what it means to be 'paused' from your applications point of
// view
// then force the director to pause (animations, scheduling, touch, etc ...)
[[CCDirector sharedDirector] pause];
}
after, when the iOS hands you back control by placing your application in the forground as the running application:
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(#"<%#>:applicationDidBecomeActive - received signal, resuming sharedDirector.",[self class]);
[[CCDirector sharedDirector] resume];
}
You dont really need to do anything fancy here other than restart the CCDirector, since your menu is in the 'Resume' state, garanteed. When user presses Resume, you will start your game again and put the menu in the 'Pause' state.

Related

How to properly minimize an iOS sprite kit application?

I've tried this iOS Sprite Kit tutorial and have created a similar app. However, I notice that when I press the home button to go to iOS home screen, I get a bad access exception in xCode. When I go back into the app, it starts from the beginning.
How can I properly close/minimize a Sprite Kit app to avoid that exception?
I tried this within the view controller presenting the scene, but it does not get called:
-(void)viewWillDisappear:(BOOL)animated
{
SKView * skView = (SKView *)self.view;
skView.paused = YES;
[super viewWillDisappear:animated];
}
I found out that there's Kobold Kit sprite engine built on top of Sprite Kit, after porting my project to that, i can minimize the app and restore it with the same stuff on the screen!
I believe that the proper way to handle minimizing an app is in the AppDelegate via these methods :
- (void)applicationWillResignActive:(UIApplication *)application
{
// 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.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

Screen Suddenly Turns Black

While using my app, the screen sometimes suddenly and inexplicably turns black. The screen is still powered, and if I play with the volume controls, the volume indicator shows just fine.
The only way to resolve this is to press the home button, at which point I see the following message in my log:
SpringBoard[15] : Failed to snapshot WaveDeck
When I then open the app again, it's fine.
Can anyone shed some light on this?
Update from the WaveDeck front:
We've used applicationDidEnterBackground in the app's delegate in a bad way: asking all the background operations to finish up using beginBackgroundTaskWithExpirationHandler and keeping the main runloop running inside applicationDidEnterBackground.
That way the method of applicationDidEnterBackground came to an end only after finishing all background operations, whether the app became active again or not.
So apparently the OS could not 'snapshot' the app correctly and would put on a black screen some time after returning from background state - when the delegate function of applicationDidEnterBackground ended.
I encounter a similar issue. Put the long time save operation in next runloop, so applicationDidEnterBackground return immediately, the system's snapshot mechanism works correctly.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// don't block ios's snapshoting, avoid screen suddenly turns black
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(saveAppState) object:nil];
[self performSelector:#selector(saveAppState) withObject:nil afterDelay:0];
}
- (void)saveAppState
{
[self longtimeSave];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(saveAppState) object:nil];
}

Is there a way in iOS to observe the home button being pushed?

I am basically trying to make the device play a sound whenever the app exits (even if the phone is being turned off, the sound would play while the button is being held down). Is this possible?
You could try the - (void)applicationWillResignActive:(UIApplication *)application UIApplicationDelegate method.
Alternately, you could listen for the UIApplicationWillResignActiveNotification notification.

Can I suppress system alerts in my app?

Is it possible for my app to "take over" when a system alert pops up? My app disables the idle timer, but when a system alert pops up, the alert seems be enabling the timer. What can I do about that?
Would it be possible for you to hook into one of the methods that gets called when your app goes in and out of the background? Say, one of these?
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(#"applicationWillResignActive");
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(#"applicationDidEnterBackground");
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(#"applicationWillEnterForeground");
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(#"applicationDidBecomeActive");
}
I bet one of those gets called when that happens. You can probably disable the idle timer when your app gets the focus back.
EDIT: On re-reading the question, it looks like you're looking to suppress the alerts (i.e., not let them happen). Heh, I focused on the latter half of your question.
Are you trying to suppress the alerts? Then the answer is: you can't.
If you're trying to prevent the alerts from messing with your app's disabling of the idle timer, then I believe donkim is on the right trail.

Awake from sleep event on the iPhone?

Is there any way to detect if the iPhone wakes up from sleep while you're app is running? Eg: your app is running, the user locks the screen (or the screen auto locks) and some time later the user unlocks the screen and up pops your app. Is there some way to get an event at that point or detect it somehow?
I've tried searching the Google and this forum, but I can't seem to find anything about it.
See applicationDidBecomeActive: on UIApplicationDelegate.
Stick these in you AppDelegate.m file:
-(void) applicationWillResignActive:(UIApplication *)application {
NSLog(#"Asleep");
}
-(void) applicationDidBecomeActive:(UIApplication *)application {
NSLog(#"Awake");
}
#Kevin - Nothing wrong with your answer - thanks by the way. Just thought I'd save the next person a Google search.