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

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.

Related

Pause/Unpause Jump Effect

I am making a game were the player sprite must jump over incoming obstacles.I have implemented a pause button and have set the un-pause function to occur when the user taps anywhere on the screen. My problem is that when the user tries to un-pause the game, he both un-pauses and makes the player jump. I have included all player movement in a tap gesture while all the button functions (pause, restart etc.) in the touchesEnded function. I also tried putting both in the touchesEnded but I saw no better result. Thank you all in advance.

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.

Pausing iOS app game when User clicks Home Button

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. :)

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.

How to make a single sprite untouchble in cocos2d?

I am developing a game with cocos2d. Naturally, I have a menu button.If it's clicked , there will be "void" called , where I stop all current actions with [[CCDirector sharedDirector] pause]; and introduce a menu.
Also, I have sprites that are stopped and have instructions if they are touched (just some MoveTo actions). When I click on sprites during pause there appears a mistake(as I think, because of that instruction).
So, I assume, that sprites should be untouchable during game pause. How can I make a single sprite untouchable? Are there better ways to avoid such a mistake?
Once you use [[CCDirector sharedDirector] pause]; then your complete cocos2d is paused. So if you have written some cocos2d actions inside the touch callback or written something to do like move sprite etc on touch then you will encounter a crash. Instead try to stop all actions and schedulers on the scene and proceed further. You can stop all actions and schedulers as below:
[self pauseSchedulerAndActions];//This will pause all the scheduler and actions running on current scene. Note this will not unschedule the update().
To stop the update method as well do-
[self unscheduleUpdate]; //This will stop the cocos2d update method
Once you paused this way now you can perform any action on the sprite and once done then you can resume as below:
[self resumeSchedulerAndActions];
[self scheduleUpdate];

Resources