当前位置 : 主页 > 手机开发 > 其它 >

Cocos2d-x系列二:xcode中导入cocos2d_libs到自己项目中

来源:互联网 收集:自由互联 发布时间:2021-06-13
xcode项目中引入cocos2dx,需要先导入cocos2d_libs静态库。 1、把cocos2dx项目(cocos2dx在Mac上开发环境配置及新建工)中如下图所示资源拷贝到xcode工程根目录下 图1-1 2、在xcode工程中导入coco

xcode项目中引入cocos2dx,需要先导入cocos2d_libs静态库。

1、把cocos2dx项目(cocos2dx在Mac上开发环境配置及新建工)中如下图所示资源拷贝到xcode工程根目录下


图1-1

2、在xcode工程中导入cocos2d_libs.xcodeproj


图1-2

注意optiens选择如下


图1-3

3、在项目中配置中选择targets,在Build Phases中添加Target Dependencies和Link Binary With Libraries


图1-4

4、PROJECT中设置Header Search Paths,相对路径,因为我把cocos2d文件夹放在MyCocos.xcodeproj的同级目录,所以是这个路径。$(SRCROOT)是.xcodeproj所在位置


图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步


图1-6

6、设置预处理的宏定义,Debug和Release,这里是以Iphone版本


图1-7
USE_FILE32API CC_TARGET_OS_IPHONE COCOS2D_DEBUG=1 CC_ENABLE_CHIPMUNK_INTEGRATION=1

图1-8
USE_FILE32API CC_TARGET_OS_IPHONE CC_ENABLE_CHIPMUNK_INTEGRATION=1

7、C / C++编译器的选择,这个与原本项目有关。


图1-9

8、关闭BitCode

上面完成之后,就可以尝试Build了,如果运气好的话,基本就可以成功了。

9、导入Classes和Resource


图1-10


如果直接导入Classes文件和Resource,运行异常如下


图1-11

检测导入后Complie Souces


图1-12


10、导入cocos2dx项目 proj.ios_mac->ios下的几个文件


图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下开发生成静态库添加到项目)


屏幕快照 2017-06-19 下午4.21.11.png

参考
xcode中导入cocos2d_libs到自己项目中
Cocos2d-x在xcode下开发生成静态库添加到项目

网友评论