公司的项目需要接入游戏业务,需要在现有的项目中嵌入Cocos2d-x。在网上找到的文章资料都比较老旧,自己总结并记录一下自己的嵌入过程。
1.下载Cocos2d-x包
下载地址:
http://www.cocos2d-x.org/download/version#Cocos2d-x
这个页面有不同的版本,根据自己的需求下载并解压;
2. 生成iOS项目
- 解压后的Cocos2d-x包下,运行
setup.py,在终端中执行python setup.py,过程中会有两次需要输入路径的操作,直接return继续。 - 执行完毕之后,再执行
cocos脚本:cocos new <项目名称,如:mygame> -p <包名,如:com.your_company.mygame> -l <开发语言:lua或cpp或js> -d <项目目录,如:NEW_PROJECTS_DIR>。 - 最后,在当前目录下,会自动生成一个
NEW_PROJECTS_DIR文件夹,该目录下就是生成的mygame项目。
3. 拷贝Cocos2d-x资源
1. 现有的项目:NewCocos,此项目作为之后做开发用的
2. 拷贝mygame项目中的cocos2d-x文件和Classes文件至NewCocos项目的根目录下



3. 在NewCocos根目录下新建Resources文件夹,并将mygame项目中的res、src、config.json拷贝到NewCocos项目中的Resources文件夹中。


4. 导入NewCocos项目
1. 导入Classes和Resources资源




2. 导入cocos2d_libs.xcodeproj、cocos2d_lua_bindings.xcodeproj、libsimulator.xcodeproj




5.添加支持库

6.项目配置(Build Settings)
- 关闭 Bitcode
- 添加
Header Search Paths

6. 收尾
因为cocos中存在AppDelegate,为了避免冲突,将项目中的AppDelegate改KMAppDelegate,并在main.m中,修改AppDelegate为KMAppDelegate
7. 验证
1. 创建控制器RootViewController
/// .h文件
@interface RootViewController : UIViewController
@end
/// .mm文件
#import "RootViewController.h"
#import "cocos2d.h"
#import "platform/ios/CCEAGLView-ios.h"
#include "scripting/lua-bindings/manual/CCLuaEngine.h"
#import "AppDelegate.h"
@interface RootViewController()
@end
@implementation RootViewController
AppDelegate* s_sharedApplication;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
s_sharedApplication = new AppDelegate();
cocos2d::Application *app = cocos2d::Application::getInstance();
// Initialize the GLView attributes
app->initGLContextAttrs();
cocos2d::GLViewImpl::convertAttrs();
//cocos2d::EventListenerCustom *_listener;
CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [UIScreen mainScreen].bounds
pixelFormat: (__bridge NSString *)cocos2d::GLViewImpl::_pixelFormat
depthFormat: cocos2d::GLViewImpl::_depthFormat
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0 ];
// Enable or disable multiple touches
[eaglView setMultipleTouchEnabled:NO];
[self.view addSubview:eaglView];
cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView((__bridge void *)eaglView);
cocos2d::Director::getInstance()->setOpenGLView(glview);
//run the cocos2d-x game scene
cocos2d::Application::getInstance()->run();
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[backBtn setTitle:@"返回" forState:UIControlStateNormal];
[backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(backEvent) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBtn];
backBtn.frame = CGRectMake(20, 100, 60, 44);
}
- (void)backEvent {
cocos2d::Director::getInstance()->end();
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.presentingViewController dismissViewControllerAnimated:true completion:nil];
});
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
delete s_sharedApplication;
s_sharedApplication = nullptr;
cocos2d::Application::sm_pSharedApplication = 0;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
auto glview = cocos2d::Director::getInstance()->getOpenGLView();
if (glview)
{
CCEAGLView *eaglview = (__bridge CCEAGLView *)glview->getEAGLView();
if (eaglview)
{
CGSize s = CGSizeMake([eaglview getWidth], [eaglview getHeight]);
cocos2d::Application::getInstance()->applicationScreenSizeChanged((int) s.width, (int) s.height);
}
}
}
@end
2. 在ViewController.m中创建一个按钮,添加点击事件
- (IBAction)jump:(id)sender {
RootViewController *vc = [[RootViewController alloc] init];
[self presentViewController:vc animated:true completion:nil];
}
完毕。。。