Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to incorporate storyboards into a cocos2d 2.0 project?

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!

like image 569
sbru Avatar asked Jul 18 '12 21:07

sbru


2 Answers

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.

like image 185
sbru Avatar answered Nov 03 '22 16:11

sbru


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

like image 40
loekTheDreamer Avatar answered Nov 03 '22 17:11

loekTheDreamer