CocosViewController – How to use Storyboards and Cocos2d V3+

I will write this tutorial in English, so, sorry for the language errors ;)

Some weeks ago I wrote a simple class for Cocos2D v3+ to use Cocos2D and Storyboards called CocosViewController.

CocosViewController is a UIViewController subclass that create CCDirector and put it in the controller view.

So, with this class we can have a Cocos2D view controller inside a Storyboard environment but we can also add UIKit elements above the Cocos2D view.

So, start this little tutorial.

How to use CocosViewController.

Create a new project (or use an existing one, but I recommend you to start with a new one to become familiar with the class) with SpriteBuilder, you can use the latest 1.2 version with Cocos2D v3.2.

cvc_1

Remove AppDelegate class and replace with the one in the CocosViewController project. Then copy CocosViewController.h/mm files in your project directory and add it in the xcode project.


cvc_2

cvc_3

Now locate the file named main.mm and rename the @”AppController” string with @”AppDelegate”.
Note: you can also rename the AppDelegate class, but for this tutorial this is the fast way.

cvc_4

Now add a new Storyboard to the project, disable Autolayout and update the project to use iOS 6.0+ and the storyboards.

cvc_5 cvc_6

At this point you can create a new Class called SceneViewController as subclass of CocosViewController.
Add the following lines to the controller class.

@interface SceneViewController() <CocosViewControllerDelegate>
@end

@implementation SceneViewController

– (void)viewDidLoad
{
[super viewDidLoad];

self.delegate = self;
}

– (IBAction)dismiss:(id)sender
{
[self removeDirector];

[self.navigationController popViewControllerAnimated:YES];
}

#pragma mark CocosViewControllerDelegate

– (CCScene *)cocosViewControllerSceneToRun:(CocosViewController *)cocosViewController
{
return [CCBReader loadAsScene:@”MainScene”];
}

@end

You can watch at the screenshot.

cvc_7

Now go to the storyboard. Add a navigation controller, a menu view controller with a button and add the SceneViewController.
Link the navigation controller to the menu controller and the menu button to the SceneViewController with a push segue.
Now you only need to create an exit button in the SceneViewController and link to the -dismiss: method of the same controller.

cvc_8

Build and Run. If you followed all the steps you have now Cocos2D inside of a Storyboard app.

I hope you enjoy it and if you have some problems you can write here in the comments or in the Cocos2D forum post.

Leave a Reply

*

10 comments

  1. KN

    Any chance you will update your tutorial for SpriteBuilder 1.3.6?

  2. I’m working on a new version, but at the moment i’m very busy. I’m hope to release new version soon.

  3. KN

    Thank you, will look forward for your next update. And thanks for creating this tutorial in the first place :))

  4. Almo

    You say “disable autolayout”. Does this mean I can’t use your class and autolayout for the UIKit stuff at the same time?

  5. At the moment you can try to enable autolayout but it’s not 100% supported.

  6. Almo

    Ok, thanks! :)

  7. Michael Simard

    Hi Dimitri,

    Thanks for sharing this. This is very helpful. I have a question. I am using your tutorial to launch a Cocos 2d scene using Cocos2d 3.2 . It works great on devices. However, on the simulator I get a black screen and looping error messages saying

    OpenGL error GL_INVALID_ENUM detected at -[CCGLView swapBuffers] 286

    I was wondering if you are aware of a quick fix for this, or at least an explanation on what could be causing it.

  8. ST

    Hi!
    I downloaded this directly from GitHub. When I run it off the bat, the scene doesnt load? suggestions? I am running on Xcode 6.1

    PS. I am new to Cocos2D, started like less than a week ago.

  9. RamblasPro

    Hi, any joy with the latest version of spritebuilder? I’m struggling getting it to work with the latest version.

    Any help would be fantasic, a tutorial like this even better ;-)

    Thanks in advance

  10. Hi, I’m very sorry for the long times for this reply. At the moment I’m unable to update the project or test it for a bugfix.

    Hope to do it very soon. Sorry again.

Next ArticleSviluppare un'applicazione per iOS