How to incorporate storyboards into a cocos2d 2.0 project?

后端 未结 3 1444
再見小時候
再見小時候 2020-12-30 16:03

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 (

相关标签:
3条回答
  • 2020-12-30 16:22

    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

    0 讨论(0)
  • 2020-12-30 16:46

    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

    0 讨论(0)
  • 2020-12-30 16:48

    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.

    0 讨论(0)
提交回复
热议问题