evraenne
evraenne

Reputation: 1

Push a Viewcontroller in a cocos2D scene

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 !

Upvotes: 0

Views: 1910

Answers (2)

Himanshu Gupta
Himanshu Gupta

Reputation: 207

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

Upvotes: 1

Guru
Guru

Reputation: 22042

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];

Upvotes: 1

Related Questions