I have a UISplitviewController app that's structured like the picture in this question:
UISplitViewController on iPad with Storyboards?
When I click on either Game #1 or Game #2, their respective game run.
However, if I click on the second game (either #1 or #2), I get this
error:
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'You can't run an scene
if another Scene is running. Use replaceScene or pushScene instead'
How can I check to see if there's already an existing Cocos2d scene
and replace it when the second game is clicked? Ideally, I think I should keep both games running so
the user can click back to the other game. Is
this possible?
I tried to simply replace this line: [director runWithScene:scene];
with this line: [director replaceScene:scene];
Unfortunately, I get an error CCTextureAtlas.m, in method:
-(void) drawNumberOfQuads: (NSUInteger) n fromIndex: (NSUInteger) start {...}
[Update]
I think I might need to stop the scene so everything is cleaned up properly. When I click on my Help screen, for example, my controller's viewWillDisappear gets called. This does not happen when switching between Cocos2d view controllers.
After following these instructions, and only creating the EAGLView once, I almost have it working, but I get a black screen for the second scene. Note that I do briefly see the new scene.
How can I check to see if there's already an existing Cocos2d scene
and replace it when the second game is clicked?
if ([[CCDirector sharedDirector] runningScene] == nil) {
[[CCDirector sharedDirector] runWithScene:sceneToRun];
} else {
[[CCDirector sharedDirector] replaceScene:sceneToRun];
}
Related
I am building an app that transitions to an end scene when two object collide. That part works just fine, but when i try to transition back to the original scene with the restart button on the end scene.The app crashes. It makes it back to the original scene but crashes immediately.The the line that is selected in the debugger is where i declare the physics body of one of the objects that collide(works fine first run. I have seen this error many times. But no one can give me an answer of how to fix it. Here is the code where i transition from the original seen to the end scene
func mineSubCrash(Mine : SKSpriteNode, subm: SKSpriteNode){
subm.removeFromParent()
Mine.removeFromParent()
self.view?.presentScene(RestartScene())
}
very basic, here is the transition back
func restart(){
restartButton.removeFromSuperview()
self.view?.presentScene(GameScene(), transition: SKTransition.fadeWithDuration(0.3))
}
I dont understand why it crashes. must be something with me not ending the original scene right . I have had this problem with other apps i have tried to make. help needed and appreciated!!
I am developing an iOS game using sprite kit with an iAd banner, and this iAd is causing the game to unpause itself, and by unpausing I mean SKActions are continuing, here are some details:
1) I have several objects in my game that have a sequence (in series not parallel) of SKActions.
2) clicking the 'pause game' button on my game's screen and 'resuming' work fine - everything is pauses as desired. NSLog statement in "Note" below not displayed.
3) I've coded the following notification which has an observer that calls a selector to pause the game (which gets called properly). NSLog statement in "Note" below not displayed.
-(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
[[NSNotificationCenter defaultCenter] postNotificationName:#"iAdsoPauseGame" object:self];
return YES;
}
4) iAd appears lalala... everything is paused as it should be. NSLog statement in "Note" below not displayed.
5) when I click the "x" in the iAd, the following occur in this order:
a) the SKAction that my object in game was running while I 'paused' the game by going to iAd goes to completion, and the NSLog statement (in Note below) IS listed.
b) around 0.5sec later, my ViewController's "viewDidAppear" is called
c) less than 1ms after 5b, my iAd's "bannerViewActionDidFinish" is called (I have a notification here that tells my game to pause ... problem is there is this 0.5sec downtime between me closing the iAd and this method being called...)
Note: In my game's main .m file, in the "update" method (called every frame), I have an NSLog statement (below), and this does display as soon as I click the 'x' in the iAd, but then stops (when 5b/5c are called).
if (NodeGamePlay.paused==FALSE) {
NSLog(#"NodeGamePlay.paused==FALSE");
}
So my question: How can I prevent the SKaction from continuing (step that occurs in 5a) when I exit the iAd? Thank you for your time and help - this has been bugging me for quite some time!
just try this one
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
skView.scene.paused = YES;
return true;
}
I am developing a Cocos3d based iOS project. I am running a scene once the app is launched and clicked on "Start scene" option. I have a Back button in the scene. Clicking on this Back, will take us to app home screen. Clicking on again "Start scene" option, currently it launches the previous scene itself. But, whenever Back button is clicked , i want to stop the running scene and unlock all the resource. And then again "Start scene" option, it should launch scene freshly.
Could someone advise, how to stop the running scene and unload all resources pod etc.?
Thank you!
You can try the following to stop displaying a Cocos2D CCScene (including any 3D scenes being displayed), and releasing all cached objects:
CC3Texture.shouldCacheAssociatedCCTextures = NO; // Breaks links between Cocos3D textures and Cocos2D textures
[CCDirector.sharedDirector end]; // Removes active CCScene and clears Cocos2D caches
[CC3OpenGL.sharedGL clearOpenGLResourceCaches]; // Clears Cocos3D caches
If you are using a CC3ViewController subclass, and you don't expect to need any OpenGL for a while, then you can also use the terminateOpenGL method to perform all of the above, plus completely shutdown OpenGL. See the CC3DemoMultiScene demo app for examples of doing this.
I have a 100% reproducible crash here.
Crash if backgrounding cocos2d 2.1 app in iOS7 while watching Game Center screens (leaderboard, achievement). It crashes instantly when pushing the home button.
Crash on line 275 in CCGLView.m:
if(![_context presentRenderbuffer:GL_RENDERBUFFER])
The itching thing is, I downloaded a fresh copy of official cocos2diphone 2.1 the other second, installed its templates and ran the staple application after hooking it up to the same app id as my problematic app that already have game center leaderboards etc set up. It does not crash. So I ran a diff on the cocos2d folders inside lib, and there is no difference except I added some C functions code in CCDrawingPrimitives.h/m... Should not be the problem.
So the problem should not be in cocos2d itself but somehow the use of it or my project setup causes it.
Update:
The problem seems to be in the cocos2d app template in use in 2.1 and possibly earlier. It looks like this:
-(void) applicationDidEnterBackground:(UIApplication*)application
{
if( [navController_ visibleViewController] == director_ )
[director_ stopAnimation];
}
And the obvious fault here is that if you have navController open a Game Center controller, then when pushing the home button the visibleViewController of director_ will be the GC controller, hence the stopAnimation will not get called. This results in a crash with iOS7, but doesn't seem to with iOS6... nor the template cocos2d 2.1 app (still confused here).
The current fix is to comment out if( [navController_ visibleViewController] == director_ ) in order to have stopAnimation always called. Not sure if there are any side effects with that but will go with this for now.
Good Call. I ran into the same issue as you do and your post helped me to figure out how my app crashed.
So my solution is stop director animation before showing game center.
[[CCDirector sharedDirector] stopAnimation]
[[CCDirector sharedDirector] presentViewController:gcViewController animated:YES completion:nil];
Then restart animation in the game center view dismiss callback
- (void) gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController
{
[[CCDirector sharedDirector] dismissViewControllerAnimated:YES completion:nil];
[[CCDirector sharedDirector] startAnimation];
}
Your updated solution should work, but same here, I am not sure if there would be any side effects. I guess it is a safer approach to just wrap around the game center itself.
Thanks again for posting this question!
I have created one level in cocos2d and i have a pause button on screen . clicking on pause screen it will open Menu ( Resume, Restart, Settings) . i want that when i click on restart menu my level will start from start. what i have tried i have remove that layer class and called it again but it didnt work. i tried to replaceScene . it didnt work either. how can i achieve that?
I have tried this and it works.
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] pushScene:[HelloWorldLayer node]];
But is it write that i am calling pushScene over and over.? will it effect my game. coz till now i am now removing that HelloWorldLayer that i want to restart again.
You have to use replaceScene. With pushScene the app will eventually run out of memory as previous scenes are not deallocating.
Be sure to create a new scene (as in your code sample), do not try to call replaceScene with the already running scene, that will fail.
You should also verify that your scene deallocates after replaceScene. Set a breakpoint in the dealloc method. If it does not deallocate, this means the scene is leaking and that can lead to all kinds of strange issues.