简体   繁体   English

在非cocos2d项目中包含cocos2d

[英]Include cocos2d in non-cocos2d project

I did some googling and stackoverflowing, but still remained with the question what to do. 我做了一些谷歌搜索和堆栈溢出,但仍然有问题该怎么做。 I have a big navigation-based project, iOS5.1, for iphone. 我有一个基于导航的大项目,iOS5.1,用于iPhone。 On couple of VCs i need to include some more or less complicated animations, like a chewing animation of a boy character. 在几个VC上,我需要包含一些或多或少复杂的动画,比如一个男孩角色的咀嚼动画。 I've worked with cocos2d and i know it would be pretty easy to accomplish my goal with cocos2d. 我和cocos2d一起工作,我知道用cocos2d完成我的目标很容易。 But is it possible to include somehow the scenes and layers with coco2sd into needed VC? 但是有可能以某种方式将coco2sd中的场景和层包含到所需的VC中吗? Or somehow to overlap the view with s scene? 或者以某种方式将视图与s场景重叠? Any hint would be highly appreciated. 任何提示都将受到高度赞赏。

The way I've done this is just creating a CCDirector when I init the ViewController, the CCDirector uses an OpenGL view that you make a subview of your ViewController. 我这样做的方法就是在初始化ViewController时创建一个CCDirector,CCDirector使用一个OpenGL视图来制作ViewController的子视图。 With the CCDirector in place you can do everything you would normally do in Cocos2D. 使用CCDirector,您可以完成通常在Cocos2D中执行的所有操作。

if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
    [CCDirector setDirectorType:kCCDirectorTypeMainLoop];

CCDirector *director = [CCDirector sharedDirector];

EAGLView *glview = [EAGLView viewWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];

[self.view insertSubview:glview atIndex:1];
[director setOpenGLView:glview];

Firstly, you need to create a director in your AppDelegate: 首先,您需要在AppDelegate中创建一个导演:

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

  // Create Cocos2D Director
  //
  // Try to use CADisplayLink director
  // if it fails (SDK < 3.1) use the default director
  if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
    [CCDirector setDirectorType:kCCDirectorTypeDefault];

  CCDirector *director = [CCDirector sharedDirector];
}

And in your -viewDidLoad method, add the view which is created by Cocos2D to self.view as a subview: 在您的-viewDidLoad方法中,将Cocos2D创建的视图添加到self.view作为子视图:

- (void)viewDidLoad {
  [super viewDidLoad];

  // configre subviews
  // ...

  // Add your Cocos2D view
  EAGLView * glView = [EAGLView viewWithFrame:self.view.bounds
                                  pixelFormat:kEAGLColorFormatRGB565
                                  depthFormat:0];
  [glView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
  [[CCDirector sharedDirector] setOpenGLView:glView];
  [self.view addSubview:glView];

  // Run a blank scene for testing
  CCScene * blankScene = [CCScene node];
  [[CCDirector sharedDirector] runWithScene:blankScene];
}

You can create your own scene to replace the blankScene then. 您可以创建自己的场景以替换blankScene


I'd highly recommend you a post on this tech: "How To Integrate Cocos2D and UIKit" . 我强烈推荐你关于这项技术的文章: “如何整合Cocos2D和UIKit” I implement this successfully by reading @Ray's post! 我通过阅读@ Ray的帖子成功实现了这一点! ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM