【问题标题】:How to incorporate storyboards into a cocos2d 2.0 project?如何将故事板合并到 cocos2d 2.0 项目中?
【发布时间】:2012-07-18 21:13:08
【问题描述】:

我在 cocos2d 2.0 中制作了一个项目,并希望使用情节提要合并一个主菜单。

我在 tinytimgames.com 上尝试过 Jerrod Putnam 的教程(我无法提供链接,因为每个帖子只允许新用户 2 个链接,但如果你在谷歌上搜索“cocos2d 故事板”它是第一个链接) 但这对我不起作用。我完全按照它(我认为)。我打开我的 cocos2d 项目并从他的 github、CCViewController.m 和 .h 导入文件,然后创建一个新的故事板文件并按照教程进行操作。但是,当我运行它时,它只是直接在我的 cocos2d 游戏上启动,而不是在我刚刚创建的新菜单上。

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

这是我从 fidgetware 尝试的另一个教程:http://fidgetware.com/Tutorials/page15/page15.html 我完成了本教程,但我的项目没有名为 RootViewController(.m 或 .h)的文件,因此我不确定将应该放入这些文件的代码放在哪里。

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

如果有人能给我一个解决方案,说明为什么这些都不适合我,或者给我一步一步详细介绍如何将故事板合并到我的 cocos2d 2.0 项目中,我将不胜感激。另外我的另一个问题是我应该从 cocos2d 2.0 项目开始并合并故事板,还是应该从单视图应用程序项目(或不同的项目?)开始并合并我的 cocos2d 2.0 部分。提前致谢!

【问题讨论】:

    标签: objective-c cocos2d-iphone xcode4.3 uistoryboard xcode-storyboard


    【解决方案1】:

    好的,在杰罗德·普特南 (Jerrod Putnam) 的大力帮助下,我终于弄明白了,非常感谢杰罗德!首先去他的教程:

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

    并从 github 链接下载和导入文件。然后创建 CCViewController 的子类并命名为 cocos2dViewController。在 cocos2dViewController.h 中复制并粘贴:

    #import "CCViewController.h"
    
    @interface cocos2dViewController : CCViewController
    
    @end
    

    并在 cocos2dViewController.m 中复制并粘贴(来自 Putnam 的教程)

    #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
    

    您会注意到我导入了 GamePlay.h,这是因为 GamePlay.m 是我拥有游戏所有内容的地方。所以为你的游戏导入头文件。你也会看到我打电话

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

    确保将“GamePlay”替换为包含您的游戏的场景名称。完成后,转到您的 AppDelegate.m 并替换您的

    application didFinishLaunchingWithOptions
    

    这个功能:

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

    你快到了!现在,对于您的故事板文件,请按照提供的链接中的 Putnam 教程进行操作。在他说“并将其类分配给我们刚刚创建的类”的地方,将其分配给 cocos2dViewController。就是这样!运行该项目,它应该可以工作,如果您有任何问题,请随时提出。

    【讨论】:

      【解决方案2】:

      你会爱我的。

      按照本教程http://www.tinytimgames.com/2012/02/07/cocos2d-and-storyboards/

      那么你必须在这里设置你的主要故事板

      诀窍是将 iOS 应用程序中的主故事板定位到您在“项目名称”/summery/iPhone/iPod 部署信息下创建的故事板。在那里选择你的男人故事板

      然后在您的 AppDelegate.m 中删除代码,使其看起来像这样:

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

      如果你不正确,我会给你我的工作源代码,请告诉我

      【讨论】:

      • @OscarApeland 我很抱歉,我再也没有回复你。不幸的是,我不再有那个项目,几乎不记得我当时所做的任何事情(现在团结一致)。请接受我的道歉。
      【解决方案3】:

      感谢 bagelboy,我只有一个更新:

          // 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];
      

      对于我来说,[[[UIApplication sharedApplication] keyWindow] bounds] 不起作用,我使用了self.view.frame

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-25
        • 1970-01-01
        • 2012-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多