5

我已经在cocos2d 2.0中创建了一个项目,并且想要使用故事板来合并主菜单。如何将故事板合并到cocos2d 2.0项目中?

我已经试过杰罗德Putnam的教程在这里tinytimgames.com(我不能提供的链接,因为新用户可在每个岗位只有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部分。提前致谢!

回答

8

好吧,我设法最终弄清楚了Jerrod Putnam的很多帮助,所以谢谢你Jerrod!第一次去他在这里的教程:

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

和下载,并从GitHub的链接导入文件。然后创建CCViewController的子类并将其称为cocos2dViewController。在cocos2dViewController.h中复制并粘贴:

#import "CCViewController.h" 

@interface cocos2dViewController : CCViewController 

@end 

并在cocos2dViewController中。米复制并粘贴(从普特南的教程)

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

确保用包含你的游戏场景的名称来代替“游戏”。一旦你这样做,去你AppDelegate.m以及与此更换你的

application didFinishLaunchingWithOptions 

功能:

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

你几乎没有!现在为你的故事板文件在提供的链接中按照Putnam的教程。他在说“并将其类指派给我们刚刚创建的类”时,将其分配给cocos2dViewController。就是这样!运行该项目,它应该工作,如果不是随便问你有任何问题。

3

你会爱上我。

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

那么你得在此处设置

,关键是要瞄准你的主要情节串连图板在你的iOS应用程序到一个你的主要故事板创建,在“项目名称” /夏日/ iPhone/iPod部署信息。选择你的男人的故事板有

然后在AppDelegate.m删除代码,以便它看起来像这样:

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


return YES; 
} 

,如果你不来的权利,我会给你我的工作的源代码,只是让我知道

+0

你可以发布一个工作项目下载链接吗? – 2013-08-08 12:48:43

+0

@OscarApeland我很抱歉,我从未回复过你。不幸的是,我不再有这个项目,几乎不能记得我当时所做的任何事情(现在团结起来)。请接受我的道歉。 – 2016-04-13 20:11:00

0

感谢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

相关问题