My app is designed to use Cocos2D in the "main game screen" but the rest of the screens are in UIKit. While I feel that this isn't ideal, I really don't have time to port the rest of the screens into Cocos2D for the time being.

One of the problems I face is that I am required to load all the textures each time I enter the main game screen, and discard them once I leave Cocos2D-land (and repeat the process all over again)... or am I?

I am actually wondering if I could preload all the textures needed in a CCTextureCache during startup, even when the EAGLView and context hasn't yet been set up until I visit the main screen (and in doing so preserve the texture cache contents). But whenever I try this the app crashes.

Any way in which I can do this?


How are you implementing mixing UIKit with Cocos2D?

The approach I use is to create a UINavigationController in the application didFinishLaunching... method and add the Cocos2D director as its root view controller.

navigator = [[UINavigationController alloc] initWithRootViewController:director];
[navigator setNavigationBarHidden:YES];

This way, using standard methods on the navigation controller, you can freely swap between UIKit view controllers and whatever Cocos2D is displaying without fully "shutting down" Cocos2D and flushing all its resources.

  • \$\begingroup\$ How would that work if I had to show a regular UIViewController subclass at startup? What I had done was to push into another UIViewController, one of whose views had to call this on itself: [[CCDirector sharedDirector] setOpenGLView:self]. \$\endgroup\$
    – Aldrich Co
    Feb 12 '13 at 3:05
  • \$\begingroup\$ Add the other view controller with initWithRootViewController, then use [navigationController pushViewController:[CCDirector sharedDirector] animated:NO] to display your Cocos2D scenes, and [navigationController popViewControllerAnimated:YES] to return to UIKit. \$\endgroup\$
    – johngirvin
    Feb 12 '13 at 8:55
  • \$\begingroup\$ For reference, UINavigationController documentation. \$\endgroup\$
    – johngirvin
    Feb 12 '13 at 8:57
  • \$\begingroup\$ I finally got it to work! Since I have a UIViewController subclass that owns the EAGLView, I kept that UIViewController instance around and then switched to it when I have to go into Cocos2D-land. But your code example would have errors in it. You can't pass a Cocos2d director to initWithRootViewController:. \$\endgroup\$
    – Aldrich Co
    Feb 12 '13 at 10:56
  • \$\begingroup\$ CCDirector extends UIViewController on iOS in 2.x - I think this changed from 1.x. \$\endgroup\$
    – johngirvin
    Feb 12 '13 at 11:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .