如何将故事板合并到cocos2d 2.0项目中? [英] How to incorporate storyboards into a cocos2d 2.0 project?

查看:143
本文介绍了如何将故事板合并到cocos2d 2.0项目中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在cocos2d 2.0中做了一个项目,并且想要使用故事板来合并主菜单。



我试过Jerrod Putnam的教程在tinytimgames.com(我不能提供链接,因为新用户只允许每个帖子2个链接,但如果你google cocos2d storyboard它是第一个链接)
但它没有为我工作。我跟着它(我想)。我打开我的cocos2d项目,并从他的github,CCViewController.m和.h导入文件,然后创建一个新的故事板文件,并按照教程。但是当我运行它,它刚刚开始我的cocos2d游戏,而不是我刚刚创建的新菜单。



我也试过这个教程:
< a href =http://zackworkshopios.blogspot.com/2012/06/cocos2d-with-storyboard-example.html> http://zackworkshopios.blogspot.com/2012/06/cocos2d-with-storyboard-example .html
,但是它对我没有用,因为我没有(或不知道在哪里找到/获取)libcocos2d.a和libCocosDenshion.a文件。



这是我尝试从fidgetware的另一个教程: http://fidgetware.com/教程/page15/page15.html
我做了这个教程,但我的项目没有一个名为RootViewController(.m或.h)的文件,所以我不知道放在哪里应该去的代码



还有Ray Wenderlich的教程,但他不使用故事板。



给我一个解决方案,为什么没有这些是为我工作,或者给我一步一步详细的运行如何将我的cocos2d 2.0项目中的故事板,我将非常感激。另一个问题是,我应该开始一个cocos2d 2.0项目,并且包含故事板,或者我应该从单视图应用程序项目(或不同的一个?),并纳入我的cocos2d 2.0部分。提前感谢!

解决方案

好吧,我终于找到了很多来自Jerrod Putnam的帮助,谢谢杰罗德!首先在这里参考他的教程:



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



,并从github链接。然后创建一个CCViewController的子类,并调用它cocos2dViewController。在cocos2dViewController.h中复制并粘贴:

  #importCCViewController.h

@interface cocos2dViewController:CCViewController

@end

并且在cocos2dViewController.m副本(来自Putnam的教程)

  #importGamePlay.h
#importcocos2dViewController.h

@interface cocos2dViewController()

@end

@implementation cocos2dViewController

- (id)initWithNibName: *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self){
//自定义初始化
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

CCDirector * director = [CCDirector sharedDirector];

if([director isViewLoaded] == NO)
{
//创建Cocos2D将渲染的OpenGL视图。
CCGLView * glView = [CCGLView viewWithFrame:[[[[UIApplication sharedApplication] keyWindow] bounds]
pixelFormat:kEAGLColorFormatRGB565
depthFormat:0
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];

//将视图分配给director。
director.view = glView;

//初始化其他控制器设置。
[director setAnimationInterval:1.0f / 60.0f];
[director enableRetinaDisplay:YES];
}

//将视图控制器设置为director的代理,以便我们可以响应某些事件。
director.delegate = self;

//将director添加为此视图控制器的子视图控制器。
[self addChildViewController:director];

//将导演的OpenGL视图添加为子视图,以便我们可以看到它。
[self.view addSubview:director.view];
[self.view sendSubviewToBack:director.view];

//完成我们的视图控制器包含的职责。
[director didMoveToParentViewController:self];

//运行任何我们想在这里运行的场景。
if(director.runningScene)
[director replaceScene:[GamePlay scene]];
else
[director pushScene:[GamePlay scene]];
}

- (void)viewDidUnload
{
[super viewDidUnload];
//释放主视图的任何保留的子视图。
}

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

@end

导入GamePlay.h,这是因为GamePlay.m是我有我的游戏的所有内容。所以导入您的游戏的头文件。此外,你会看到我调用

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

确保将GamePlay替换为包含游戏的场景的名称。一旦你这样做,请去你的AppDelegate.m并替换你的

 应用didFinishLaunchingWithOptions 



函数:

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

你差不多了!现在为你的故事板文件按照Putnam的教程在提供的链接。在那里他说并将其类分配给我们刚刚创建的,将它分配给cocos2dViewController。就是这样!运行项目,它应该工作,如果不能随意问你有任何问题。


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!

解决方案

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.

这篇关于如何将故事板合并到cocos2d 2.0项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆