0
votes

I have an app. This app combines UIKit and Cocos2D. I have a UIKit menu with a button that calls a cocos2D game. It works fine.

Now, I would like to remove the cocos scene to push the UIKit menu. (Game is over, it's necessary to go on the menu view)

I tried

[[CCDirector sharedDirector] end];
[[CCDirector sharedDirector].openGLView removeFromSuperview];

It doesn't work. I don't know how to do.

Thanks for your help !

2
what exactly does happen after you remove it from superview? does it remain on the screen or it's just a black?skytz
After I remove it from the superview, my cocos2D view is just "frozen".evraenne
have you tried adding a new view to the app's window? The way i understand it is this: a EAGLView is created and CCDirector is added to that..when the director ends you need to remove the view manually from superview ( which you did) but now the app's window has no view..so you need to add oneskytz
Which version of cocod2d do you use, 1.x or 2.x? In cocos2d 2.x CCDirector is a subclass of UIViewController.Kreiri

2 Answers

1
votes

Try this in cocos2d 2.0 and above:

To Present view controller:

AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];    

//presentModalViewController 
[app.navController presentModalViewController:leaderboardViewController animated:YES];

//dismissModalViewControllerAnimated:YES
[app.navController dismissModalViewControllerAnimated:YES];

To add subview:

AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[app.navController.view addSubview:mSegmentedControl];
1
votes

Try to do something like this. When you are pushing cocos scene on a uiviewcontroller then add this code in ViewDidLoad method.

-(void)viewDidLoad{
[super viewDidLoad];
CCDirector *director = [CCDirector sharedDirector];

if([director isViewLoaded] == NO)
{

CCGLView *glView = [CCGLView viewWithFrame:[[[UIApplication sharedApplication] keyWindow] bounds]
                               pixelFormat:kEAGLColorFormatRGB565
                               depthFormat:0
                        preserveBackbuffer:NO
                                sharegroup:nil
                             multiSampling:NO
                           numberOfSamples:0];


director.view = glView;


[director setAnimationInterval:1.0f/60.0f];
[director enableRetinaDisplay:YES];
}

director.delegate = self;
[self addChildViewController:director];
[self.view addSubview:director.view];



[director didMoveToParentViewController:self];


if(director.runningScene)
{

[director replaceScene:[SceneFirst scene]];
}
else
{

[director runWithScene:[SceneFirst scene]];
}

}

Here SceneFirst is your cocos Scene you want to push.Just add CCDirectorDelegate in your UiViewController as delegate. and add this line of code in your ViewDidUnload method

[[CCDirector sharedDirector] setDelegate:nil];

For popping back to your Uikit view call this code on any CCmenu tapped

[[CCDirector sharedDirector] stopAnimation];
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[app.navigationController popViewControllerAnimated:YES];

Hope This Help!!:)