Cocos2D and Storyboards

UPDATE: I’ve now uploaded a ready-to-use class for your enjoyment to GitHub.

Storyboarding in iOS 5 is really neat. It allows you to lay out nearly your entire interface visually, and gives you some neat features that simply make your life as a developer easier. Since our next game (an RPG) is going to be extremely UI-heavy, naturally we wanted to use Storyboarding to create our interface. The problem comes when it’s time to fire up Cocos2D and display our battle scenes.

However, with Cocos2D 2.0 (the OpenGL ES 2.0 version) and iOS 5′s view controller containment functionality, integrating Cocos2D in a Storyboard is actually very trivial.

Containing Cocos2D

Let’s start by creating a new class called CocosViewController, which derives from UIViewController. It should also conform to the CCDirectorDelegate protocol.

#import "cocos2d.h"
 
@interface CocosViewController : UIViewController <CCDirectorDelegate>
 
@end

So far, so good. Now, there’s something important to note about Cocos2D 2.0′s director, especially if you’re used to 1.x: The 2.0 director actually derives from UIViewController! This makes it incredibly easy to add a Cocos2D view to our hierarchy.

Over in the implementation file, you’ll want to set up your viewDidLoad method like so:

- (void)viewDidLoad
{
    [super viewDidLoad];
 
    CCDirector *director = [CCDirector sharedDirector];
 
    if([director isViewLoaded] == NO)
    {
        // Create the OpenGL view that Cocos2D will render to.
        CCGLView *glView = [CCGLView viewWithFrame:[[[UIApplication sharedApplication] keyWindow] bounds]
                                       pixelFormat:kEAGLColorFormatRGB565
                                       depthFormat:0
                                preserveBackbuffer:NO
                                        sharegroup:nil
                                     multiSampling:NO
                                   numberOfSamples:0];
 
        // Assign the view to the director.
        director.view = glView;
 
        // Initialize other director settings.
        [director setAnimationInterval:1.0f/60.0f];
        [director enableRetinaDisplay:YES];
    }
 
    // Set the view controller as the director's delegate, so we can respond to certain events.
    director.delegate = self;
 
    // Add the director as a child view controller of this view controller.
    [self addChildViewController:director];
 
    // Add the director's OpenGL view as a subview so we can see it.
    [self.view addSubview:director.view];
    [self.view sendSubviewToBack:director.view];
 
    // Finish up our view controller containment responsibilities.
    [director didMoveToParentViewController:self];
 
    // Run whatever scene we'd like to run here.
    if(director.runningScene)
        [director replaceScene:[MyScene scene]];
    else
        [director runWithScene:[MyScene scene]];
}

And so that we’re nice and polite programmers, we’ll clean up after ourselves:

- (void)viewDidUnload
{
    [super viewDidUnload];
 
    [[CCDirector sharedDirector] setDelegate:nil];
}

And that is quite literally all of the code that you need to write to get the basics up and running. Let me explain a little bit about what’s going on.

In viewDidLoad, we first check to see if the director’s view has been loaded. Remember that the Cocos2D director is now simply a subclass of UIViewController, so we can use methods like isViewLoaded to get information that we need. If the view hasn’t been loaded, that means we haven’t created an OpenGL view for the director yet, so we do that if necessary.

After that, we do the real magic, which uses iOS 5′s view controller containment. We first add the director as a child view controller. Doing this means that the director’s viewWillAppear:, viewWillDisappear:, et al will be called properly, and in Cocos2D 2.0, that means that the director will automatically pause and unpause itself when the view controller disappears or appears. Awesome!

We then add the director’s view (the OpenGL view) to our view controller’s view hierarchy and send it to the back. Sending it to the back allows us to place buttons and other UIKit stuff on top of the OpenGL view in Interface Builder. Finally, we need to call the director’s didMoveToParentViewController: method, as required for view controller containment. We then run our scene, and that’s it!

Adding Cocos2D to a Storyboard

Here we have a perfectly normal Storyboard, with a navigation controller and a view controller that has a simple button.

Now let’s drag a new view controller onto the canvas,

and assign its class to the one we just created.

Set up a segue between the button and our new view controller. Set the segue type to “Push” for now.

And now, just build and run! You should be able to press the button to load your Cocos2D scene, press the back button to stop it, and repeat the process over and over again! You can even add a button to the CocosViewController if you’d like.

Some Notes

As you may have noticed, Cocos2D’s initialization happens whenever CocosViewController is first pushed onto the navigation stack (aka, lazy loading). You may prefer to initialize Cocos2D right at launch to minimize the initial delay.

Also note that if you want to have Cocos2D in the background and have a navigation controller on top (so that you can have a navigation structure for your pause menu, for example), you’ll need to add another navigation controller as a child view controller. View controller containment isn’t really supported in Interface Builder, sadly, so if you do this, you’ll probably want to create another storyboard file and load it as your child view controller.

Additionally, there may be some gotchas with this code that I just don’t know about, as it’s only had minimal testing. So let me know if you run into any troubles, and I’ll try to keep the post updated.

Tags: , , ,

  • Anonymous

    Fantastic work, thanks so much for sharing this

    • guest555

      I’m having problems to do this with kobold 2d ( i thought it would be better because it will enable ARC automatically), I opened a New Kobold2d with UIKIT project, done the steps till the storyboard and got an initial error on MyScence (since i have to create a scence) so i created an empty subclass of CCScence)
      then i got ARC issure:

              [director replaceScene:[cocosScene scene]];

          else

              [director runWithScene:[cocosScene scene]];

      no Known class method for selector ‘scene’


      i need to build a cocos2d game (dunno what would be better with ARC (ios5) or not -ios4) 

      and i cant even decide on how to start building the main menu/splash screen (even tough i’ve finished ray’s book Learning cocos2D (which is directed for ios4 ,no ARC)
       

  • http://twitter.com/GeorgeSealy George Sealy

    Thanks for this Jerrod, this is really handy.  One issue I’ve had is that I’ve been getting ”displayLink must be nil. Calling startAnimation twice?” assertions when I perform the segue to my Cocos view controller.  Putting a [director stopAnimation] call in at the end of my view controller’s viewDidLoad method seems to fix them problem, but feels a bit cludgy.

    Have you had this issue?  If not, I’m not quite sure what I’ve done differently, except that my segue is modal rather than a push.  I wonder if this could affect the order some delegate methods are being called?

    • http://www.tinytimgames.com/ Jerrod Putman

      Hey George,

      Ah yes, I did run into that. What I did was use “pushScene:” instead of “runWithScene:”. I actually think this is a bug in Cocos2D. “runWithScene:” simply calls “pushScene:” and then “startAnimation”, but whenever the director is presented as a view controller (which is the way you’re supposed to do it now), “viewWillAppear:” is called, which also calls “startAnimation”. So it wouldn’t work anyways!

      I’m going to send a pull request to try and get that problem fixed. In the meantime, you can totally use pushScene: (all of the Cocos2D tests use it actually, which is why they don’t all have a problem with startAnimation being called twice). Just be careful when popping scenes not to pop all of them off the stack. Currently, Cocos2D doesn’t handle that very well.

  • Brennan Sarich

    This is awesome, thanks!  I’m going to share this idea with my dev, I was just looking at articles on this since I’m building an rpg-style game myself!  =D

  • YJL

    How did you add the cocos2d code to a Cocoa Touch project in the first place? I crated a single view-based app and copied the lib folder from a Cocos2d-Box2d template project. But, I got tons of errors. Xcode seemed to fail to find cocos2d/box2d files. 

    Thanks in advance. 

    • http://www.tinytimgames.com/ Jerrod Putman

      You can look here for how I usually set up my Cocos2D projects: http://www.tinytimgames.com/2011/07/22/cocos2d-and-arc/

  • IS

    Hi, thanks for your effort,

    It says

    “It should also conform to the CCDirectorDelegate protocol.”

    but your header file does not confirm to it ,can you please explain ,I get lot of errors after I follow this

    • http://www.tinytimgames.com/ Jerrod Putman

      The header file I posted on Github conforms to the protocol. I’ll update the post to fix the error.

      What errors are you getting? Are you using Cocos2d 2.x?

  • http://fidgetware.com/ Mick Lester

    Thanks for this tutorial.  All works as expected, but I want to force the cocos2d layer to be in landscape mode.  I added 
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    }But it is still in portrait mode.  Any suggestions?

    • http://fidgetware.com/ Mick Lester

      Another issue is when I do rotate the device the cocs2d screen is not changing its size.    I have a white section on the side, which is the background of the ViewControllers view.
      I found this bug
      http://code.google.com/p/cocos2d-iphone/issues/detail?id=1310
      which seems to be what I am seeing.

      • http://www.tinytimgames.com/ Jerrod Putman

        I managed to get it rotating by adding the method you posted above. Not sure why you would be having the problem. Make sure you place the auto-rotation code in your CCViewController-derived class.

        As for the rotation bug, yeah, that’s a Cocos2D problem. You could try using the auto-rotation code that Cocos2D 1.x uses to resize the EAGLView, or wait for a fix.

  • Rhuantavan

    I am wondering if this is possible with Kobold2d as well? Probably not until 2.0 is out?

  • http://fidgetware.com/ Mick Lester

    It was rotating but the position of the view was not correct. I have it working now with cocos2d V1.

    • http://www.tinytimgames.com/ Jerrod Putman

      Ah, okay. Well, at least you got it working somehow. To be perfectly honest, the game I’m using this solution on is a portrait only game, so I wasn’t aware of the issue.

  • Jacks

    This is pretty basic, but is there a way for the running scene to perform segues?

    The situation I have is this.  Say I have two scenes – one is quest dialogue which is a UIViewController. The other is a battle scene which has animations so is a CCScene.  From the quest dialogue I segue to the custom view controller described here.  That custom view controller calls the director to run my Battle Scene.  This works great.

    Now, I want to be able to segue back to the quest dialogue screen when the battle is over (for example, if the battle is won, to have a continue button that moves to the next dialogue, or if the player selects a menu and gives up, to reset).  But the Battle Scene is not a view controller, so it can’t segue (I think). Or create UIKit buttons, etc.  Any help appreciated!

    • http://www.tinytimgames.com/ Jerrod Putman

      So first of all, you need to use your custom view controller to orchestrate all of these separate pieces. It would be in control of dismissing or popping itself back to your dialogue screen. Your battle scene would have to inform your view controller that the battle is done, and the controller would then handle the rest.

      You should also be able to place UIKit buttons and elements onto your custom view controller in Interface Builder. The code here is set up so that the Cocos2D layer is placed behind any views, buttons, or controls that might have been placed in IB. If you need your battle scene to respond to IBActions, you would have to have your view controller forward that interaction to your battle scene.

      For our game, we’re actually using the view controller as the main driving force behind the battle system, which then basically tells the nodes in the scene what they should be doing. In fact, we have no custom CCScene/Node/Sprite classes as we’re literally just using Cocos2D to render battle scenes (and for its excellent CCAction library).

      Hope that helps!

  • Nader

    Thank you for this great post. It would be really cool if someone could help to add a sample test app to your github which would use your library and also add the iAd ad viewing stuff to it…

  • Shai

    I dont understand why it does not work for me.  I open a new cocos2d 2.0 project, import the the two files from github, create a storyboard file and follow this tutorial exactly.  It doesn’t work for me, it just starts up with the cocos2d default “hello world” page and there is no way for me to see the new menu i created.  Any help would be greatly appreciated.  Thanks

    • http://www.tinytimgames.com/ Jerrod Putman

      You need to set your storyboard as the Main Storyboard file in your Target settings (or instantiate and display the storyboard yourself), and you need to remove all of the Cocos2D initialization from the AppDelegate.

      • Shai

        Thanks for the reply and I’m not quite sure how to remove all and only the cocos2d initialization in the AppDelegate.  I don’t know which lines pertain only to cocos2d.   Could you explain a little bit more on that?  Thanks

        • http://www.tinytimgames.com/ Jerrod Putman

          If you’re using one of the Cocos2D templates, then basically everything in the AppDelegate is Cocos2D initialization code. If you set up your storyboard in the Target settings, then you can have an application:didFinishLaunchingWithOptions: that simply returns YES. The app would then display your storyboard immediately upon app launch.

          • Shai

            Thanks again for the reply, I tried that and not it almost works.  On startup it shows the menu but it isn’t rotated correctly.  Also when I click the cocos2d button to start the hello world scene, nothing shows up its just a black screen.

          • http://www.tinytimgames.com/ Jerrod Putman

            Are you supplying a shouldAutorotateToInterfaceOrientation: in your CCViewController subclass, and is it returning YES for the orientations you support?

            As for the scene not appearing, I’m not certain. You may want to run your scene with pushScene: instead of runWithScene: due to some issues with how CCDirector handles this. Otherwise, I don’t really have any ideas.

          • Shai

            Yeah, i have that shouldAutorotate in my CCViewController.m file and in my AppDelegate.m file but it is still 90 degrees off like in the picture above.  

            And I imported your CCViewController.m and .h files directly from github and your .m does not have any pushScene or runWithScene calls.  So i replaced the viewDidLoad function in the .m file from your github with the viewDidLoad function on this page, but I still can’t get it to work.  I dont know what to replace for “MyScene scene”.  Do I put the cocos2d game scene or the main menu scene or neither of those?  Thanks again for all the help.

          • http://www.tinytimgames.com/ Jerrod Putman

            For loading your scene, you need to subclass CCViewController, and override viewDidLoad (making sure to call super). That’s where you call pushScene:

            As for the rotation issues you’re still having, I don’t really know what could be going on. The app I’m using this in only supports portrait, so I haven’t tested how the autorotation functions. Make sure you also have shouldAutorotate in your child view controllers as well. Otherwise, I really have no idea why you’re having these problems.

          • Shai

            Can you elaborate on that first paragraph please? Sorry I am very new to this and I dont understand what you mean.  Thanks

          • Sbruhis

            When you say “you need to subclass CCViewController” does that mean I go to file>new>file>objective-c class> and then make sure it says “subclass of CCViewController”? Or do you mean I need to make a new objective-c class that is a named “CCViewController” and is a “subclass of UIViewController”?  

            And by “overriding viewDidLoad” does that mean I replace the viewDidLoad function in CCViewController.m with the viewDidLoad function at the top of this page, or does it mean that in my new objective-C class that is subclassed from CCViewController I put the viewDidLoad function at the top of the page?

            And what do I replace for “MyScene scene” I can’t seem to figure that part out.  Any more feedback and help is greatly appreciated.  I’ve been stuck on this for 2 weeks now :/

          • http://www.tinytimgames.com/ Jerrod Putman

            1) Yes, subclass of CCViewController.
            2) Place the viewDidLoad in your class that you created in 1). Remember to call [super viewDidLoad] in this method.
            3) The replacement for [MyScene scene] would be your own Cocos2D scene.

            Really, this is a bit of an advanced setup, and you really need to know both Cocos2D and UIKit very well to use it properly and you need to be able to talk back and forth between Cocos2D and UIKit.

  • Edjimarin

    Since I’ve been already developing an iPhone app, I thought it would be pretty cool to add a bit of 2D game features to it.  So I was looking around, and found that Cocos2d was just the ticket, and saw that you had created a ViewController that would all me to just create a new ViewController of Cocos2d along with all my others.  So I took the Cocos2D-2.0 Xcode file and dropped it into my own project, and brought over your CCViewController into my app as well. 

    Everything seems to be building OK, but when I try to create my own MyCCVIewController : CCViewController, I’m getting a linker error of “”CCViewController” not found… This maybe a trivial problem, but how do you get the right path when using a Xcode build file?  I’ve put your CCViewController.h file under the Cocos2D Xcode directory… 

    Any help would be appreciated …

    Thanks,

    Ed

    edjimarin@yahoo.com

    • Shai

      Hey there, so just to clarify, you started with an xcode single view project and then imported the CCViewController .m and .h files and your cocos2d 2.0 project file?

  • Shai

    I was gonna reply but it was way too indented so im starting up here again.  Also, i am not sure how to correctly display code in a thread so sorry about that in advance.  OK so I have my App Delegate which calls 

    [director_ pushScene: [IntroLayer scene]]; Then my introLayer does this:

    -(void) makeTransition:(ccTime)dt
    {
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[MainMenu scene] withColor:ccBLUE]];
    }Then I have MainMenu.h and .m but they are pretty much empty except .m has:

    @implementation MainMenu

    // Helper class method that creates a Scene with the GamePlay as the only child.
    +(CCScene *) scene
    {
    // ‘scene’ is an autorelease object.
    CCScene *scene = [CCScene node];

    // ‘layer’ is an autorelease object.
    MainMenu *layer = [MainMenu node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
    }
    @endThen I have a storyboard which uses a navigation controller and I want the opening scene to be the main menu when i call the ‘makeTransition’ function.  In my storyboard I have a Play button on this main menu.  This play button is push segued to another view controller which is of class ‘CCViewController’.  I have the CCViewController.m and .h from github imported and then i made a subclass from it called cocos2dViewController.m and .h.  In my cocos2dViewController.m I call:

        if(director.runningScene)

            [director replaceScene:[GamePlay scene]];

        else

            [director pushScene:[GamePlay scene]];

    GamePlay.m and .h are where i define my whole game.

    So when I run it I get the launch screen and then it transitions with the color blue to a black screen.  Is this because there is nothing in MainMenu.m? Also, (I think) I shouldn’t need anything in MainMenu.m. I just need some way of making the MainMenu scene be the initial (main menu) scene of the storyboard.  I feel that I am pretty close to getting this to work and I really appreciate all the help you have given me Jerrod.  Any advice/knowledge you have to offer at this point or if you need any more info to diagnose the problem would again be greatly appreciated.  Thanks! 

    • Shai

      So, I guess my question is how do I make my MainMenu scene be my initial view controller?  Thanks

      • Shai

        I managed to make it my initial view by making the applicationDidFinishLaunching in appdelegate.m with:

        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
        {
            return YES;
        }And now everything works except when i click the play button it goes to a black screen.  I replaced pushScene: with runWithScene: but it still doesn’t work.  Any ideas?

  • Shai

    Well I finally figured it out.  Thanks Jerrod! Here is a link to my answer of how I managed to make it work:

    http://stackoverflow.com/questions/11550437/how-to-incorporate-storyboards-into-a-cocos2d-2-0-project/11801085#11801085

  • Moon

    Excellent tutorial, thanks a lot! I have a question, you write:

    “As you may have noticed, Cocos2D’s initialization happens whenever
    CocosViewController is first pushed onto the navigation stack (aka, lazy
    loading). You may prefer to initialize Cocos2D right at launch to
    minimize the initial delay.”

    How would I initialize Cocos2D right at launch? Do I need to move all the code from the viewDidLoad function into the AppDelegate? Sorry if this is a stupid question, I’m a N00B :)

    • Trung

      @Moon,
      This is what I did:
      I move all the init code of cocos2d to AppDelegate and replace the viewDidLoad of CCViewController with this one:

      - (void)viewDidLoad
      {
      [super viewDidLoad];

      CCDirector *director = [CCDirector sharedDirector];
      director.delegate = self;

      // Add the director as a child view controller.
      [self addChildViewController:director];

      // Add the director’s OpenGL view, and send it to the back of the view hierarchy so we can place UIKit elements on top of it.
      [self.view addSubview:director.view];
      [self.view sendSubviewToBack:director.view];
      [director pushScene:[IntroLayer scene]];
      // Ensure we fulfill all of our view controller containment requirements.
      [director didMoveToParentViewController:self];

      }

      The trick is that the CCViewController is a UIViewController, and what we need to do is just set the delegate of director to CCViewController then add the view of director as a subview of CCViewController view. It isn’t affected by where/when you init the cocos2d director.

      Trung

      • Jose Lulz

        “move all the init code of cocos2d to AppDelegate” how do it?

        i move the method? or where put in?

        • Trung

          yeah, just copy all the init code to a function and call it on CCViewController viewDidLoad

  • Candy

    When I run my program, the cocos2d view controller always runs first, is there a way i can have a regular UIViewController, say one that holds buttons for a menu, load before the cocos2dViewController?

  • Jeff S.

    I followed this tutorial and added a menu screen using stroyboard and a viewcontroller, and have a button in the menu screen that then sends me to the cocos2d view controller game, however, when i start up the app, all that is shown is a black screen any ideas whats going wrong?

  • dyego

    I’m trying to use more than one CCviewControler subclass, but it doens’t work. I initialize the storyboard with the cocosViewControler, the same code you write in this post.
    it’s call my cocos scene.
    So at the end of my CCscene I call the next storyBoard like this:

    UIStoryboard *story = [UIStoryboard storyboardWithName:@"MainStoryBoardPad" bundle:nil];
    UIViewController *uiviewControler = [story instantiateViewControllerWithIdentifier:@"menuView"];
    [[[CCDirector sharedDirector] view] addSubview:uiviewControler.view];
    [[CCDirector sharedDirector] replaceScene:[CCScene node]];
    So my storyboard is called right way. Now the problem begins. I put a button on it and a create another subclass of CCviewControler, but when I try to put this class in the new storyboard, it doens’t show my new class. I want to know if anyone knows how I can handle two subclasses of CCViewControler.

    • Trung

      hi dyego
      Did you find any solution?

  • mv

    thank you for your tutorial.
    I did it like that, but now I’m getting a white page and many, many errors like:

    OpenGL error 0×0506 in -[CCSprite draw] 532

    OpenGL error 0×0502 in -[CCGLView swapBuffers] 280

    • mv

      ok, found the solution on another page:

      Here is what was hanging me up:

      - (void)viewDidLoad {
      [super viewDidLoad];
      CCGLView *__glView = [CCGLView viewWithFrame:

      [[[UIApplication sharedApplication] keyWindow] bounds]
      pixelFormat:kEAGLColorFormatRGBA8
      depthFormat:0 /* GL_DEPTH_COMPONENT24_OES */
      preserveBackbuffer:NO
      sharegroup:nil
      multiSampling:NO
      numberOfSamples:0
      [m_director setView:__glView];
      [self.view addSubview:m_director.view];
      };

      Changed to: [CCGLView viewWithFrame:

      self.view.frame

  • mv

    still one comment: I did it like described here, but it asked me for libcocos2d.a and libCocosDenshion.a and more, which couldn’t be found, when I build my own project. So you have to build first the whole cocos2D-project inside your own project. That will produce all the libs like libcocos2d.a and more. Also I had to add the lib: libz.dylib in my own project (link binary with libraries). That is not written here in the tutorial next to libcocos2d.a and so on.

  • Pierre

    Great!!

    And if you want your glView to resize:

    CCGLView *glView = …
    glView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    • IHaveNoClueAboutProgramming

      Nice little gem! Has helped me solve a problem with this implementation and autorotation in iOS6! Kudos!

  • caridee

    Hi~this is such a great tutorial!!
    but however I’m trying to put storyboard into a cocos2d project and I’m wondering if it’s possible??

  • http://www.facebook.com/ogamerboy Vitor Santana

    hi guys, i have a problem on my cocos2d with storyboard project. i changed all my viewcontrollers to landscape view, but when i call cocosviewcontroller, device keeps on landscape mode, but scene keeps on portrait view. in other words, put cocos2d scene by side!! help me !!

    thanks !!.

    • Maxim Zaks

      this is the only thing that worked for me

      CGRect windowFrame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width – 1);

      CCGLView *glView = [CCGLView viewWithFrame:windowFrame

      - 1 sets it to the right orientation. I have to investigate why this is happening though.

  • Will

    Is there any reason this wouldn’t work with iPhone 5? On my simulator, it works fine, but when I open the scene on my iPhone 5, the pixels get all mixed up and only some layers show. I’ll attach screen shots.

    Thanks for your tutorial!

    • Will

      I removed the line:

      [director enableRetinaDisplay:YES];

      and it seems to be working right, but still 1 black line going across the right of the screen. Will update if i figure it out.

      • Will

        Got the black line to go away by calling

        [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGB5A1];

        after

        [director setAnimationInterval:1.0f/60.0f];

        Hope this helps someone out. Let me know if I’m doing something wrong, as I am just starting out with Cocos2d and just trying to get things to work! :

  • Trung

    Does anyone experience that CCLOG doesn’t print out anything when we do this?
    My situation is as below:
    In the AppDelegate, I init the navigationController with a MenuViewController(which is a UIViewController).
    Then from the MenuViewController, I set up a button to load up the CCViewController. My game is working just fine but the CCLOG doesn’t show up anything. I did a small test with NSLog and it print out normally.
    I’m suspecting that the way we feed UIViewController to the navigationController would change the CCLOG console.

    Does anyone know a hint on this?

    Thanks in advance!
    Trung

    • Trung

      I figured this out. I hit the problem when I started a completely new apple project without cocos2d lib and integrated the cocos2d myself as an static lib.

      Solution: add COCOS2D_DEBUG = 1 in build setting of cocos2d static lib.
      Post up hope to help some one like me :)

  • Trung

    Thanks very much!

    This turns out really easy to put CCLayer as a background and put UI item on the top as user interface.
    Really good article!

  • Anibal

    HI!

    I’m facing a problem that is getting me crazy.

    I executed all this code, initializing Cocos2D either in the AppDelegate or in the UIViewController. In both cases, when I call the UIViewController via performSegue:, the view appears OK, but no animation is started.

    When I minimize the app, just in that moment that I press the Home button, the application starts animating, and works fine since that moment. When I reopen the app, everything seems to be working fine.

    So, I think it should be some initialization issue, or (?) some other stuff.

    Has anyone face this issue? Does anyone know how to solve it?

    I’m using Cocos-2D 2.x, XCode 4.6.1 with iOS 6.1

    Thanks!

  • Brendan

    Thanks for a great tutorial. Would it be possible to upload an example e.g. a basic file with a uikit viewcontroller and a ccviewcontroller. i am having trouble getting cocos2d set up without using the templates (which i cannot do if i would like to use the storyboards).