Cocos2D not updating after view popped and pushed - iphone

I'm writing an iPhone game which has a number of levels. During play the user can use a menu to quit the game and return to the level select screen. To do this I'm using a UINavigationController with series of UIViewControllers. When the player chooses to quit, the game view is popped from the stack and the level select menu is displayed. The game runs fine the first time through but if the player quits the level and then tries to play the same or another level this causes a problem.
The game view controller (which displays the cocos2d scene) is a member variable of a game controller singleton. When the user clicks "play again" this game controller resets the game state. The cocos2d layers are cleared and the game is reset to it's starting condition. After this the game view is pushed onto the stack again. This time however cocos2d doesn't update. The screen is just the last frame from the previous game frozen.
It seems that for some reason when the cocos2d view is popped and then pushed it stops updating even though I use:
[[CCDirector sharedDirector] resume];
Does anyone have any experience of this problem and how it could be avoided?

When your game view controller is popped the [[CCDirector sharedDirector] stopAnimation] is called, so after pushing your game view controller onto the stack again you must call the [[CCDirector sharedDirector] startAnimation].
Hope this helps.

I would try it like that:
[self resumeSchedulerAndActions];
for(CCSprite *sprite in [self children]) {
[[CCActionManager sharedManager] resumeTarget:sprite];
}

Related

Keeping data, switching scene cocos2d

In a simple game, I preload approximately 100 sprites onto a CCBatchNode, and then modify their visibility and position during the game. When the game is over I want to switch to a different scene to show High Scores, and then go back and play again. Is there a way to avoid having to reload all the sprites onto a new BatchNode?
I was also wondering how it would be best to store things like coins, that can be collected each game. Would NSUserDefaults be the best way to go?
I think best solution is to use a push scene to push your scene that shows the high scores and then use popscene to pop the high scores scene from the stack. When you push and pop a scene, the underlying scene is unchanged.
[[CCDirector sharedDirector] pushScene:[HelloWorldLayer scene]];
[[Director sharedDirector] popScene];

Cocos2d access scene properties and methods

I'm trying to include a pause feature for my game. So far running pause on the [CCDirector sharedDirector] has been good enough when run through my pauseGame method (which incorporates a BOOL to tell if game is paused, etc.)
However, I noticed that if I go back to the homescreen on my device, my application delegate will automatically run pause and resume on the sharedDirector.
Ideally, I would like access my active scene/layer's so I can run my own pause method.
How can I: 1) check if the current scene is my game scene
2) access the game scene's pause property, and run the pauseGame method on it?
Any help appreciated. Thanks
The running scene is [CCDirector sharedDirector].runningScene
If you call [scene pauseSchedulerAndActions]; on the current scene this is will not be resumed when going back and forth on the home screen.
If you need to pause ALL nodes in the hierarchy, here is a method to extend CCNode
-(void)recursivePauseSchedulerAndActions {
[self pauseSchedulerAndActions];
CCNode *child;
CCARRAY_FOREACH(children_, child) {
[child recursivePauseSchedulerAndActions];
}
}
-(void)recursiveResumeSchedulerAndActions {
[self resumeSchedulerAndActions];
CCNode *child;
CCARRAY_FOREACH(children_, child) {
[child recursiveResumeSchedulerAndActions];
}
}
I use a different approach for pausing a game.
I usually have a game scene with game layers. To pause the game I add a new layer (covering the entire screen) and when I pause the game I display this layer. This way you can stop touching events for the game layer and restore them when resuming the game (and removing the pause layer from the scene)

Unbalanced calls to begin/end appearance transitions for <GKModalRootViewController: 0xb7e450>

I give up on that point, I just can't figure out what is wrong and where...
Here is the problem: in my iPhone application using Cocos2d, I configured autorotation through a viewController; however, since, when Game center opens its view as the user taps on "Create new account" during the authentication, this view does not receive any touch, but the touch go to the game's view (which is hidden under the Game center view).
I have tried everything I thought about, but since I did not find any callback about this Game Center View, it is hard to find a way to correct this...
Here is the initialization of the game's view:
// Init the UI View Controller
//
viewController = [[SQViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;
EAGLView *view = [EAGLView viewWithFrame:[window bounds] pixelFormat:kEAGLColorFormatRGBA8 depthFormat:GL_DEPTH_COMPONENT24_OES];
[director setOpenGLView:view];
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
[view removeFromSuperview];
[viewController setView:view];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
I have tried many other things, commented every single line in this code, tried some others (such as setHidden:NO, bringSubviewToFront...), but the only results I could get were:
- Game display ok, Game Center ok, but no autorotate
- Game displayed in portrait (the view controller only allow landscape modes), Game Center ok, no autorotate
- Game not displayed (black screen), Game Center ok
And no way to make it all work together... The only clue I have is the title of this topic, "Unbalanced calls to begin/end appearance transitions for ." But since I do not call the Game Center view myself, I don't know what to do with this...
Anyone, any idea?
I've had the same problem while displaying game center leaderboards in my cocos2d built App ever since moving up to iOS 5.0. I've seen references elsewhere to this being caused by a sub viewcontroller losing focus on the parent viewcontroller, but I've been unable to verify that or get this resolved in my app either.
Good news is that I've run this thru instruments - No Memory Leaks. Also executed the same action repetitively with no apparent failures or ill effects.
So while this message is an annoyance, it doesn't appear (at least for now) to adversely affect the App.
This Error occurs when you try to push a viewController before previous ViewController is finished . Means you are trying to push 2 ViewControllers at the same time.

MPMoviePlayerViewController Not Rotating until app restart

K here is an interesting problem
We have an app that is presenting with unusual behavior
If you install that app (from either test flight or via xcode debug) onto a "clean" device (where the app has not existed before or has been previously delete) when the app plays a mp4 from the web or from local memory. The MPMoviePlayerViewController used to play the app will not rotate.
However if you force quit the app and start it again, everything rotates correctly. Even if you quit and return without closing the background thread the video will not rotate.
Here is the simple code
mediaController = [[MPMoviePlayerViewController alloc] initWithContentURL: [NSURL fileURLWithPath: videoPath]];
[self presentMoviePlayerViewControllerAnimated: mediaController];
[mediaController release];
Any Ideas?
It might be that one of the root view for another one of the tabs is stopping the root view containing your player from rotating. The UITabBarController asks all its children view controllers (one for each tab) whether it should allow rotation and will only allow one to rotate to a given orientation if all of them allow it. This is done so as to avoid the orientation switching violently from one to another when the user switches tabs. Try putting a breakpoint in the shouldAllowAutoRotation for all of the UITabBarController's children and see if they get called.
I discovered the issue with this. the UIWindow can only have one view. If another is added, 2nd views will not receive the notification. I had an action that was triggering on the first load that was doing this causing the views to not rotate.

Iphone game development switching between scenes

i have created a pause button on touch the scene is replaced by the pause menu....
and when we resume the scene comes back...however the function keep calling themselves....
how can i pause every thing in my current file when i switch to another scene...
"on touch", the running scene should call:
[[CCDirector sharedDirector] pushScene: [PauseMenu scene]];
The pause menu's "back to game" button should then call:
[[CCDirector sharedDirector] popScene];
Pushing the pause menu into place, should "pause" the scene that is currently being pushed back and when you pop it back it should continue running.
Documentation: http://www.cocos2d-iphone.org/api-ref/latest-stable/interface_c_c_director.html