前语

在上一篇文章中现已向大家介绍了如何获取一个cocos2d-x 4.0版别游戏引擎的空包。接下来便是如何将coco2d-x的项目与原生项目合体。

弥补阐明一点:

更新xcode15后,缺少No matching function for call to ‘iconv’文件

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

像这样强制转换下就能够正常编译过去了

iconv_close((iconv_t)_iconv);
iconv((iconv_t)_iconv, (char**)&pin, &inLen, &pout, &outLen);

开始集成到Swift项目中

特别阐明:在集成过程中需求非常的耐性和细心,假如有些装备没有处理,就会导致各种奇奇怪怪的无法编译,发动闪退等等各种置疑人生的问题!需求耐性一步步检查排错!只是集成到主项目就花费1天多的时间。

若是Object-C的项目,防止AppDelegate文件重名,请现将项目中的改一个其他的名字。比方:XXAppDelegate。

导入必须的文件到主工程

1.复制Class类

如箭头所示的Class文件,作用在于用发动游戏引擎的必要文件。

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

2.复制资源文件

2.1 在你的Swift项目中新建一个Source的空文件夹,名字能够自定义不影响。

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

2.2 将下面3个挑选的文件复制到Source中。

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

3.增加到主工程中

3.1 Class文件的增加需求挑选Create groups

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

3.2 Source文件的增加

3.2.1 现将挑选的SourceClasse一样先挑选Create groups

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

3.2.2 打开Xcode挑选Source文件中的三个文件,点击Delete

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

3.2.3 点击References

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

3.2.4 点击从头增加到主工程,并且挑选箭头所指方法。切记不要选错了!!!

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

3.2.5 增加结束示意图 确认 res 和 src 是蓝色文件夹图标

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

3.3 接下来便是面临疾风吧!

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

特别阐明:假如你现已查找了其他资料,他们会让你把cocos2d-x也复制到主项目中。我试过了!不得行啊!各种问题,缺少各种依靠库。所以我摆烂了,我在研讨了2个多小时原始的Mygameo之后。做了一斗胆的操作:直接把能够运转的Mygameo 文件依靠库地址复制到我的Swift项目中!亲测有用,可能是我是coco2d-x 4.0版别吧。网上的参考资料所示的文件目录,我底子没有找到。要么便是不匹配。所以我这篇4.0的仅供参考,其他版别或者是cocos-Creator请酌情参考!依据自己的项目需求来匹配!

3.3.1 增加Target Dependencise

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

3.3.2 增加依靠库们~

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

3.3.3 装备Header Search Paths,以下装备请依照你的项目目录修正或者能够打开原项目直接复制途径防止犯错!以下途径仅供参考!

/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/runtime-src/Classes
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/cocos/scripting/lua-bindings/auto
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/cocos/scripting/lua-bindings/manual
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/cocos
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/extensions
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/cocos/platform
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/cocos/editor-support
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/cocos/base
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/cocos/audio/include
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/cocos/platform/ios
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/recast/..
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/tinyxml2/.
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/xxhash/.
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/xxtea/.
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/clipper/.
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/edtaa3func/.
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/ConvertUTF/.
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/poly2tri/..
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/md5/..
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/unzip/.
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/lua/tolua/.
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/lua/luasocket/..
$(inherited)

为了愈加直观如图所示!

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

3.3.4 装备System Header Search Paths,以下装备请依照你的项目目录修正或者能够打开原项目直接复制途径防止犯错!以下途径仅供参考!

/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/Box2D/include
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/chipmunk/include
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/freetype2/include/ios/freetype2
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/bullet/include
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/bullet/include/bullet
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/jpeg/include/ios
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/openssl/include/ios
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/uv/include
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/webp/include/ios
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/websockets/include/ios
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/curl/include/ios
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/png/include/ios
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/glsl-optimizer/include
/Users/a000/Desktop/cocos2d-x-4.0/NEW_PROJECTS_DIR/MyGameo/frameworks/cocos2d-x/external/lua/luajit/include
$(inherited)

为了愈加直观如图所示!

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

3.3.5 装备Other lunker Flags,以下装备请依照你的项目目录修正或者能够打开原项目直接复制途径防止犯错!看到的每一项都需求原封不动的粘贴到主项目里,不然报错的时分你底子查不到更多的资料,我在这块耗时3个小时+,无奈之举!

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

这些需求的lib库,也需求依照能够运转的Mygameo里逐条增加!

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

3.3.6 装备C++相关

假如未装备会报错如下:No template named 'unary_function' in namespace 'std'; did you mean '__unary_function'?

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

3.3.7 装备模拟器运转相关假如你不需求,能够跳过此过程。首要M1芯片Mac,模拟器问题。

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

解决方案:

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

3.4 编译等开奖!

假如呈现奇奇怪怪的问题,请先确保原Mygameo项目是否能够正常运转!假如打包出来能够,那么就证明有很细腻的途径装备呈现了问题。重复上述过程,细心且耐性的检查!

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)

4.发动游戏!

原理阐明:因为项目是Swift的,所以我们肯定是需求桥接文件,通过来装备Object-C的文件引证。一起,我在Swift项目中,仅保留AppDelegate.swift文件,另外一个SceneDelegate.swift我是删除了的。直接上源代码环节!

特别阐明:因为我们需求引证c++的文件,所以需求将GameStarViewController.m 改为 GameStarViewController.mm

4.1 增加桥接文件JumpGame-Bridging-Header
#import "GameStarViewController.h"
4.2 AppDelegate.swift
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = UINavigationController(rootViewController: ViewController())
        self.window?.makeKeyAndVisible()
        return true
    }
}
4.3 ViewController.swift
import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.view.backgroundColor = .red
        let label = UILabel(frame: CGRect(x: 50, y: 50, width: 200, height: 30))
        label.text = "点击任意当地跳转游戏引擎"
        label.textColor = .white
        self.view.addSubview(label)
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.navigationController?.pushViewController(GameStarViewController(), animated: true)
    }
}
4.4 GameStarViewController.mm

注:GameStarViewController.h 便是创立后的默许代码,就不粘贴了。

#import "GameStarViewController.h"
#import "cocos2d.h"
#import "platform/ios/CCEAGLView-ios.h"
#include "scripting/lua-bindings/manual/CCLuaEngine.h"
@interface GameStarViewController ()
@property (strong, nonatomic) CCEAGLView *eaglView;
@end
@implementation GameStarViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.blueColor;
    cocos2d::Application *app = cocos2d::Application::getInstance();
    // Initialize the GLView attributes
    app->initGLContextAttrs();
    cocos2d::GLViewImpl::convertAttrs();
    self.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
    [self.eaglView setMultipleTouchEnabled:NO];
    // Set EAGLView as view of RootViewController
    self.view = self.eaglView;
    cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView((__bridge void *)self.eaglView);
    cocos2d::Director::getInstance()->setOpenGLView(glview);
    //run the cocos2d-x game scene
    cocos2d::Application::getInstance()->run();
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end

5.终究效果图

iOS-Swift项目嵌入Cocos2d-x游戏开发(二)