Cocos2d-x系列二:xcode中导入cocos2d_libs到自己项目中
2017-06-26 09:40
330 查看
xcode项目中引入cocos2dx,需要先导入cocos2d_libs静态库。
1、把cocos2dx项目(cocos2dx在Mac上开发环境配置及新建工)中如下图所示资源拷贝到xcode工程根目录下
![](http://upload-images.jianshu.io/upload_images/1337433-b289af5d5c5d4329.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-1
2、在xcode工程中导入cocos2d_libs.xcodeproj
![](http://upload-images.jianshu.io/upload_images/1337433-2b514db60e660f29.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-2
注意optiens选择如下
![](http://upload-images.jianshu.io/upload_images/1337433-6fe60903e804f5dd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-3
3、在项目中配置中选择targets,在Build Phases中添加Target Dependencies和Link Binary With Libraries
![](http://upload-images.jianshu.io/upload_images/1337433-49052357eef2012d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-4
4、PROJECT中设置Header Search Paths,相对路径,因为我把cocos2d文件夹放在MyCocos.xcodeproj的同级目录,所以是这个路径。$(SRCROOT)是.xcodeproj所在位置
![](http://upload-images.jianshu.io/upload_images/1337433-9db7584ae13d3f01.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-5
5、设置TARGETS中Header Search Paths,这里设置同第4步
![](http://upload-images.jianshu.io/upload_images/1337433-5db716574dd6092f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-6
6、设置预处理的宏定义,Debug和Release,这里是以Iphone版本
![](http://upload-images.jianshu.io/upload_images/1337433-87868e36e5f0d40c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-7
![](http://upload-images.jianshu.io/upload_images/1337433-042f8c582172fc12.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-8
7、C / C++编译器的选择,这个与原本项目有关。
![](http://upload-images.jianshu.io/upload_images/1337433-ef93b5ada8714161.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-9
8、关闭BitCode
上面完成之后,就可以尝试Build了,如果运气好的话,基本就可以成功了。
9、导入Classes和Resource
![](http://upload-images.jianshu.io/upload_images/1337433-5bf217f83bc81232.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-10
如果直接导入Classes文件和Resource,运行异常如下
![](http://upload-images.jianshu.io/upload_images/1337433-f5dd57a50420fb2f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-11
检测导入后Complie Souces
![](http://upload-images.jianshu.io/upload_images/1337433-49dbab12d259d50f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-12
10、导入cocos2dx项目 proj.ios_mac->ios下的几个文件
![](http://upload-images.jianshu.io/upload_images/1337433-f2380f5cec33fe36.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-13
11、修改AppDelegate名称,否则与cocos::AppDelegate冲突
12、在ViewController.cpp中开启游戏
13.使用经过编译后的libcocos2d IOS.a,避免每次运行都要编译。(Cocos2d-x在xcode下开发生成静态库添加到项目)
![](http://upload-images.jianshu.io/upload_images/1337433-d24af050f2c119d7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
屏幕快照 2017-06-19 下午4.21.11.png
参考
xcode中导入cocos2d_libs到自己项目中
Cocos2d-x在xcode下开发生成静态库添加到项目
1、把cocos2dx项目(cocos2dx在Mac上开发环境配置及新建工)中如下图所示资源拷贝到xcode工程根目录下
![](http://upload-images.jianshu.io/upload_images/1337433-b289af5d5c5d4329.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-1
2、在xcode工程中导入cocos2d_libs.xcodeproj
![](http://upload-images.jianshu.io/upload_images/1337433-2b514db60e660f29.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-2
注意optiens选择如下
![](http://upload-images.jianshu.io/upload_images/1337433-6fe60903e804f5dd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-3
3、在项目中配置中选择targets,在Build Phases中添加Target Dependencies和Link Binary With Libraries
![](http://upload-images.jianshu.io/upload_images/1337433-49052357eef2012d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-4
4、PROJECT中设置Header Search Paths,相对路径,因为我把cocos2d文件夹放在MyCocos.xcodeproj的同级目录,所以是这个路径。$(SRCROOT)是.xcodeproj所在位置
![](http://upload-images.jianshu.io/upload_images/1337433-9db7584ae13d3f01.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-5
$(inherited) $(SRCROOT)/cocos2d $(SRCROOT)/cocos2d/cocos $(SRCROOT)/cocos2d/cocos/base $(SRCROOT)/cocos2d/cocos/physics $(SRCROOT)/cocos2d/cocos/math $(SRCROOT)/cocos2d/cocos/2d $(SRCROOT)/cocos2d/cocos/ui $(SRCROOT)/cocos2d/cocos/audio/include $(SRCROOT)/cocos2d/extensions $(SRCROOT)/cocos2d/external $(SRCROOT)/cocos2d/external/chipmunk/include/chipmunk $(SRCROOT)/cocos2d/cocos/editor-support
5、设置TARGETS中Header Search Paths,这里设置同第4步
![](http://upload-images.jianshu.io/upload_images/1337433-5db716574dd6092f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-6
6、设置预处理的宏定义,Debug和Release,这里是以Iphone版本
![](http://upload-images.jianshu.io/upload_images/1337433-87868e36e5f0d40c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-7
USE_FILE32API CC_TARGET_OS_IPHONE COCOS2D_DEBUG=1 CC_ENABLE_CHIPMUNK_INTEGRATION=1
![](http://upload-images.jianshu.io/upload_images/1337433-042f8c582172fc12.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-8
USE_FILE32API CC_TARGET_OS_IPHONE CC_ENABLE_CHIPMUNK_INTEGRATION=1
7、C / C++编译器的选择,这个与原本项目有关。
![](http://upload-images.jianshu.io/upload_images/1337433-ef93b5ada8714161.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-9
8、关闭BitCode
上面完成之后,就可以尝试Build了,如果运气好的话,基本就可以成功了。
9、导入Classes和Resource
![](http://upload-images.jianshu.io/upload_images/1337433-5bf217f83bc81232.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-10
如果直接导入Classes文件和Resource,运行异常如下
![](http://upload-images.jianshu.io/upload_images/1337433-f5dd57a50420fb2f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-11
检测导入后Complie Souces
![](http://upload-images.jianshu.io/upload_images/1337433-49dbab12d259d50f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-12
10、导入cocos2dx项目 proj.ios_mac->ios下的几个文件
![](http://upload-images.jianshu.io/upload_images/1337433-f2380f5cec33fe36.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
图1-13
11、修改AppDelegate名称,否则与cocos::AppDelegate冲突
12、在ViewController.cpp中开启游戏
#import "ViewController.h" #import "GAppDelegate.h" #import "RootViewController.h" #include "cocos2d.h" @interface ViewController () { RootViewController *viewController ; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIButton * bt = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; bt.backgroundColor = [UIColor brownColor]; [bt addTarget:self action:@selector(bt:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:bt]; } //初始化 - (void)initGame { cocos2d::Application *app = cocos2d::Application::getInstance(); // Initialize the GLView attributes app->initGLContextAttrs(); cocos2d::GLViewImpl::convertAttrs(); } //开启游戏 - (void)startGame { cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView((__bridge void *)viewController.view); cocos2d::Director::getInstance()->setOpenGLView(glview); //run the cocos2d-x game scene cocos2d::Application::getInstance()->run(); } - (void)bt:(UIButton*)bt { [self initGame]; // Override point for customization after application launch. // Use RootViewController to manage CCEAGLView viewController = [[RootViewController alloc]init]; // viewController.wantsFullScreenLayout = YES; ((GAppDelegate *)[[UIApplication sharedApplication] delegate]).root = viewController; // [[UIApplication sharedApplication] setStatusBarHidden:true]; [self presentViewController:viewController animated:YES completion:^{ // IMPORTANT: Setting the GLView should be done after creating the RootViewController [self startGame]; }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
13.使用经过编译后的libcocos2d IOS.a,避免每次运行都要编译。(Cocos2d-x在xcode下开发生成静态库添加到项目)
![](http://upload-images.jianshu.io/upload_images/1337433-d24af050f2c119d7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
屏幕快照 2017-06-19 下午4.21.11.png
参考
xcode中导入cocos2d_libs到自己项目中
Cocos2d-x在xcode下开发生成静态库添加到项目
相关文章推荐
- xcode中导入cocos2d_libs到自己项目中
- 【iOS-cocos2d-X 游戏开发之七】整合Cocos2dX的Android项目到Xcode项目中,Android导入打包运行即可!
- 【iOS-cocos2d-X 游戏开发之七】整合Cocos2dX的Android项目到Xcode项目中,Android导入打包运行即可! 推荐
- iOS-cocos2d-X 游戏开发之七】整合Cocos2dX的Android项目到Xcode项目中,Xcode编写&编译代码,Android导入打包运行即可!
- 【iOS-cocos2d-X 游戏开发之七】整合Cocos2dX的Android项目到Xcode项目中,Android导入打包运行即可!
- 【iOS-cocos2d-X 游戏开发之七】整合Cocos2dX的Android项目到Xcode项目中,Xcode编写&编译代码,Android导入打包运行即可!
- Cocos2d_x系列一:Cocos2d-x在xcode下开发生成静态库添加到项目
- xcode 把cocos2d-x 以源码的形式包含进自己的项目适合, 性能分析问题的错误
- Xcode项目报错系列记录整理——000001导入svn项目出错
- 【Cocos2d-X(2.x) 游戏开发系列之二】cocos2dx最新2.0.1版本跨平台整合NDK+Xcode,Xcode编写&编译代码,Android导入打包运行即可!
- Xcode报错: pod管理项目导入第三方报错
- Cocos2dx3.0 疑难解答系列 (三) 导入android项目后 import org.cocos2dx.lib.Cocos2dxActivity 出错
- 如何将自己的项目导入到github里面
- SWTBOK測试实践系列(5) -- 项目中使用手动和自己主动化的策略
- Cocos2d-x项目移植到WP8系列之四:文件操作
- Cocos2d-x项目移植到WP8系列之五:播放MP3
- Mac下 Xcode4.6.3,cocos2d-x2.2(或者cocos2d-x2.1.5)Android,eclipse(2013 September版本)跨平台环境配置以及创建跨平台项目
- 将GitHub上下载的项目工程作为Module导入自己的项目中
- Cocos2d-x项目移植到WP8系列之七:中文显示乱码
- Android Studio 将本地开源库导入到自己的项目中使用(小记)