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

前言

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

补充说明一点:

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

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

scss 复制代码
iconv_close((iconv_t)_iconv);  
iconv((iconv_t)_iconv, (char**)&pin, &inLen, &pout, &outLen);

开始集成到Swift项目中

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

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

导入必须的文件到主工程

1.拷贝Class类

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

2.拷贝资源文件

2.1 在你的Swift项目中新建一个Source的空文件夹,名字可以自定义不影响。
2.2 将下面3个选择的文件拷贝到Source中。

3.添加到主工程中

3.1 Class文件的添加需要选择Create groups
3.2 Source文件的添加

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

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

3.2.3 点击References

3.2.4 点击重新添加到主工程,并且选择箭头所指方式。切记不要选错了!!!

3.2.5 添加完毕示意图 确定 res 和 src 是蓝色文件夹图标

3.3 接下来就是面对疾风吧!

特别说明:如果你已经搜索了其他资料,他们会让你把cocos2d-x也拷贝到主项目中。我试过了!不得行啊!各种问题,缺少各种依赖库。所以我摆烂了,我在研究了2个多小时原始的Mygameo之后。做了一大胆的操作:直接把可以运行的Mygameo 文件依赖库地址拷贝到我的Swift项目中!亲测有效,可能是我是coco2d-x 4.0版本吧。网上的参考资料所示的文件目录,我根本没有找到。要么就是不匹配。所以我这篇4.0的仅供参考,其他版本或者是cocos-Creator请酌情参考!根据自己的项目需求来匹配!

3.3.1 添加Target Dependencise

3.3.2 添加依赖库们~

3.3.3 配置Header Search Paths,以下配置请按照你的项目目录修改或者可以打开原项目直接拷贝路径避免出错!以下路径仅供参考!

bash 复制代码
/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)

为了更加直观如图所示!

3.3.4 配置System Header Search Paths,以下配置请按照你的项目目录修改或者可以打开原项目直接拷贝路径避免出错!以下路径仅供参考!

bash 复制代码
/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)

为了更加直观如图所示!

3.3.5 配置Other lunker Flags,以下配置请按照你的项目目录修改或者可以打开原项目直接拷贝路径避免出错!看到的每一项都需要原封不动的粘贴到主项目里,不然报错的时候你根本查不到更多的资料,我在这块耗时3个小时+,无奈之举!

这些需要的lib库,也需要按照可以运行的Mygameo里逐条添加!

3.3.6 配置C++相关

如果未配置会报错如下:No template named 'unary_function' in namespace 'std'; did you mean '__unary_function'?

3.3.7 配置模拟器运行相关如果你不需要,可以跳过此步骤。主要M1芯片Mac,模拟器问题。

解决方案:

3.4 编译等开奖!

如果出现奇奇怪怪的问题,请先确保原Mygameo项目是否可以正常运行!如果打包出来可以,那么就证明有很细腻的路径配置出现了问题。重复上述步骤,仔细且耐心的检查!

4.启动游戏!

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

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

4.1 添加桥接文件JumpGame-Bridging-Header
arduino 复制代码
#import "GameStarViewController.h"
4.2 AppDelegate.swift
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
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 就是创建后的默认代码,就不粘贴了。

objectivec 复制代码
#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.最终效果图

相关推荐
成长ing121388 个月前
Cocos 游戏入门 飞翔的小鸟
cocos creator·cocos2d-x
iOS阿玮9 个月前
iOS-Swift项目嵌入Cocos2d-x游戏开发(一)
cocos2d-x
upstream1 年前
cocos2d-js开发笔记
cocos2d-x