Pausing iOS app game when User clicks Home Button - ios

While the user is playing the game if they tap the Home Button on iPhone/iPad the game pauses itself but doesn't stay paused if the user clicks back on the app to open it, instead game leads them to game over screen. How do I code it that once the user clicks Home Button the game pauses and if they click back on app, within 2 seconds the game unpauses and you continue. Is there a particular code that goes inside the following methods that pauses the entire game and then unpauses it after a someone clicks on it and a few seconds go by?
UIApplicationDelegatedelegate
-(void)applicationWillResignActive:(UIApplication *)application{}
-(void)applicationWillEnterForeground:(UIApplication *)application{}
I got the app to pause in the background and stay paused but how do I make it that when the user EnterForeground again, there's about a 2 second delay before the game un-pauses?

iOS can't magically pause or un-pause your game for you - it's something you have to handle on your own in the code.
Once you have the logic to un-pause the game you can get the 2 seconds delay you mentioned in your question by calling performSelector:withObject:afterDelay: (see the docs). For example:
[self performSelector:#selector(continueGame) withObject:nil afterDelay:2.0]
where self has a method called continueGame to .. continue the game. :)

Related

Running a function when the game becomes active iOS - SpriteKit

In my game in swift / SpriteKit, I have a problem when the user quits the app and opens it up again. If the game is on the pause menu and everything is paused, when the game is reloaded the game is unpaused but the labels on the pause menu do not disappear. How do I keep the game paused when I reopen it? I have tried
func applicationWillEnterForeground(application: UIApplication) {
// 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.
let scene = GameScene(size: CGSize(width: 2048, height: 1356))
scene.view?.paused = true
}
But this is not doing anything. I have also tried pause() but that messes things up. Any help is appreciated.
You want to use the applicationWillResignActive function to pause your scene, by doing something like scene.speed = 0 which will properly pause your scene (don't know why, but it doesn't quite work just using pause). Here you can also display your pause menus (which you appear to be doing).
Then you want to use the applicationDidBecomeActive function to continue your game. Here you do your checks to see if (guessing here) your "resume" button is pressed, and in the response method for that, set the scene.speed = 1. Here your response method, do anything that requires starting back up (animations, actions, hiding your pause menu(s), or music).
I will say, these are the two functions that I use, you are free to use whichever two functions you would like that have a on/off kind of thing. I've found these to be very reliable.

How to ensure SKScene is paused when app resumed from background?

I am developing an iOS game; I am using several SKScenes and a single View Controller and my menu navigates between the different SKScenes. I'm having a problem with pausing (my game is using SKActions):
Problem: Suppose my game is in a 'paused' state (all SKActions of objects in my game are paused). When I click the 'home' button and send my app to the background, everything remains paused as it should. However, when I click my app to resume it, the scene that should be paused automatically 'resumes' itself, and all the SKActions resume to completion, and then the game pauses after this. (Please let me know if I can provide any additional details).
'Evidence': I know this because in the 'update' loop called every frame I have an NSLog statement that prints when the SKNode nodeGamePlay is paused, and it prints several times (half a second) when I click my game on the simulator's background.
What I want: When the app is clicked and opens, I want my pause-game-menu to appear, and I want my game (children of nodeGamePlay) to be paused.
What I have: In my SKScenePlay, I have a method that loads a 'pause menu' (not a child of nodeGamePlay) and pauses nodeGamePlay, which displays when a user clicks the pause button. Currently, when the user sends my app to the background and then reopens it, this method is called and the pause menu is added and the nodeGamePlay SHOULD GET paused (I call it with the following in my only viewController). However, although the pause menu displays, the nodeGamePlay does not get paused. How can I fix this?
- (void)appDidBecomeActive:(NSNotification *)notification {
[[NSNotificationCenter defaultCenter] postNotificationName:#"loadPauseGameMenu" object:scene];
}
EDIT: Update, when I set self.paused=true (for the SKScene), everything pauses correctly. When I do a for-in loop for each child in self and set each child.paused=true, SKActions are not paused correctly.

Home button does not terminate program in simulator

I'm new to programming and have been using this site for a while now but everything I've had to ask has been answered somewhere but I couldn't find an answer to this:
I am programming a game for the Apple AppStore using Xcode and test it on the iOS Simulator. When I "Build and Run the current Scheme" (the play button on Xcode), I can test out my game, and when I am done I can hit the "Stop the running scheme or application" (the stop button on Xcode), and of course it ends my game.
However, on the iOS Simulator, an icon for my game appears on the main screen along with apple programs such as "safari". When I click this, it enters my game and I can play it fine. However when I click the "Home button" or the "lock button" and re-enter my game, the game is continuing from where I left off; I want the game to end so when I re-enter my game it goes back to its original state.
I was wondering if anyone had a solution to this problem? Thank you in advance for your time and help!
When you hit the Home button on your device or simulator, you are simply "backgrounding" the app. That is, you are not quitting or killing the application, you are simply sending it to an 'idle' sort of state.
iOS will automatically kill/quit applications that are in the background if it needs memory, but what you are probably seeing is that you background your app (hit the Home button), and then you tap the game's icon to open it again. This will resume the game where it left off.
When you hit the square (stop) button in Xcode, you are killing/quitting your application. If you run your application from the simulator (not by hitting the play button in Xcode) and you want to quit the app entirely, hit the Home button twice or use the Command+Shift+H shortcut twice to show the app switcher, and then just drag your application's card up.
This will quit the app and make it launch from scratch when you tap the icon in the home screen again.
Return your game to the original state using this method in the AppDelegate.m:
- (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.
}
Now when you hit the home button and start the app next time it will be in the state you left it (however you want this to be).

Pause Screen Design iOS game

My game has a pause screen which doubles as the main title screen with high scores. It is semi-transparent so the user can see what state the game-round was in. They can see the last message such as "Win" or "Loss". In the case of an interruption or pause they see "Redo". My game already handles interruptions gracefully, saving state and restoring last round so that the user can try again.
Question; when the user closes the app (hits home button) or if the app is interrupted with an incoming phone call during game-play, when the app comes back into the foreground should I show the pause screen or should I take them back to the game? Another way to phrase this question would be, should the home button and the pause button behave virtually the same for game-play interruption? (I presume that if the user were in a sub-screen, non-game-play, they would be taken back to that sub-screen).
Question 2; What does it mean to pause? I believe all sounds should cease but can I get away with keeping some minimal animation going? I do know that if the app were to go into the background that the OpenGL ES 2 animation would have to cease. Do you think users would prefer to have no animations during pause? I question the extent to which battery life should influence the answer.
I think this is a very subjective question. I'll try to give you some input.
I would say the good rule of thumb is: Will it affect your game play if you don't pause the game? For example, if your game is a chess or sudoku game, it wouldn't really matter if you pause the game when the user exit to home screen. However if your game is an action game that each mili second is very important and will decide whether the user will win, then in that case you would need to pause the game.
What does it mean to pause: It depends on your implementation. It can be anything you want. You can even continue the animation in the background if you want. This is all up to you.

How to wait until the CCMenuItemImage is press in objective C?

I want to pause the program until my button is press
I have pause my scene by Use [[CCDirector sharedDirector] pause]; sure that the screen is pause but my function is continue running until it end function
Do they have any function to wait for the press button action??
If you pause the CCDirector, it will drop the framerate to 4 fps - it does not stop the CCDirector from running game updates, nor will it halt the execution of the current method.
Note that the low framerate will affect touch detection and you may find it hard to activate any CCMenu button while CCDirector is paused.
These are just two reasons why CCDirector's pause method is a poor substitute for a real implementation of pausing your game. In principle, when you bring up the ingame (pause) menu, what should happen is that any game play object is simply not running any updates and pauses any currently running actions. Only the foreground menu remains running normally to receive user input.

Resources