How to incorporate storyboards into a cocos2d 2.0 project?
Asked Answered
G

3

5

I have made a project in cocos2d 2.0 and would like to incorporate a main menu using storyboards.

I have tried Jerrod Putnam's tutorial here on tinytimgames.com (I can't provide the link because new users are allowed only 2 links per post, but if you google "cocos2d storyboard" it is the first link) but it did not work for me. I followed it exactly (I think). I opened my cocos2d project and imported the files from his github, the CCViewController.m and .h and then created a new storyboard file and followed the tutorial. However when I ran it, it just started straight on my cocos2d game and not on the new menu I just created.

I also tried this tutorial: http://zackworkshopios.blogspot.com/2012/06/cocos2d-with-storyboard-example.html but once again it did not work for me as I do not have (or do not know where to find/get) the libcocos2d.a and libCocosDenshion.a files.

This is another tutorial I tried from fidgetware: http://fidgetware.com/Tutorials/page15/page15.html I did this tutorial but my project does not have a file called RootViewController (.m or .h) so I was not sure where to put the code that is supposed to go into those files.

There is also Ray Wenderlich's tutorial but his does not use storyboards.

If anyone can give me a solution as to why none of these are working for me or give me a step by step detailed run through of how to incorporate storyboards into my cocos2d 2.0 project, I would GREATLY appreciate it. Also Another question I have is should I start with a cocos2d 2.0 project and incorporate storyboards or should I start with a single-view application project (or a different one?) and incorporate my cocos2d 2.0 part. Thanks in advance!

Gest answered 18/7, 2012 at 21:13 Comment(0)
G
8

Alright, I managed to finally figure it out with a lot of help from Jerrod Putnam, so thank you Jerrod! First go to his tutorial here:

http://www.tinytimgames.com/2012/02/07/cocos2d-and-storyboards/

and download and import the files from the github link. Then create a subclass of the CCViewController and call it cocos2dViewController. In cocos2dViewController.h copy and paste this:

#import "CCViewController.h"

@interface cocos2dViewController : CCViewController

@end

and in the cocos2dViewController.m copy and paste this (from the Putnam's tutorial)

#import "GamePlay.h"
#import "cocos2dViewController.h"

@interface cocos2dViewController ()

@end

@implementation cocos2dViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (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:[GamePlay scene]];
    else
        [director pushScene:[GamePlay scene]];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

You'll notice that I imported GamePlay.h, that is because GamePlay.m is where I have all the content for my game. So import the header file for your game. Also you will see that I call

if(director.runningScene)
    [director replaceScene:[GamePlay scene]];
else
    [director pushScene:[GamePlay scene]];

Make sure to replace "GamePlay" with the name of the scene which contains your game. Once you do that, go to your AppDelegate.m and replace your

application didFinishLaunchingWithOptions

function with this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    return YES;
}

You're almost there! Now for your storyboard file follow Putnam's tutorial in the link provided. Where he says "and assign its class to the one we just created", assign it to cocos2dViewController. And that's it! Run the project and it should work, if not feel free to ask any questions you have.

Gest answered 3/8, 2012 at 18:11 Comment(0)
S
3

you going to love me.

follow this tutorial http://www.tinytimgames.com/2012/02/07/cocos2d-and-storyboards/

then you gotta set your main story board here

the trick is to target your main storyboard in your iOS applications to the one you created-under "project name"/summery/ iPhone/iPod deployment info. select your man story board there

then in your AppDelegate.m remove the code so that it looks like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{


return YES;
}

if you dont come right i will give you my working source code, just let me know

Stockdale answered 3/8, 2012 at 0:38 Comment(1)
@OscarApeland im so sorry, i never got back to you. unfortunately i don't have that project anymore and can hardly remember anything i did back then (working in unity now). please accept my apology.Stockdale
R
0

Thanks to bagelboy, I have only one update:

    // Create the OpenGL view that Cocos2D will render to.
    CCGLView *glView = [CCGLView viewWithFrame:self.view.frame
                                   pixelFormat:kEAGLColorFormatRGB565
                                   depthFormat:0
                            preserveBackbuffer:NO
                                    sharegroup:nil
                                 multiSampling:NO
                               numberOfSamples:0];

For my instance, [[[UIApplication sharedApplication] keyWindow] bounds] did not work, and I used self.view.frame

Royalroyalist answered 18/2, 2014 at 21:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.