本篇基于
一.环境配置
下载cocos2d
git clone https://github.com/cocos2d/cocos2d-x.git
cd cocos2d-x
git checkout cocos2d-x-3.17.1
git submodule update --init
克隆存储库后,执行download-deps.py下载并安装依赖项。
python download-deps.py
接着执行
python setup.py
配置环境变量,这将会配置cocos运行环境
验证cocos
打开cmd命令 执行cocos -v 查看环境是否配置成功,显示如下则说明配置成功
克隆spine
git clone https://github.com/EsotericSoftware/spine-runtimes.git
cd spine-runtimes
git checkout 3.7
git submodule update --init
二.创建项目
使用如下命令创建cocos项目
cocos new 项目名称 -p (iOS bundle id或Android包名) -l cpp -d 存储位置路径
等待项目创建成功
替换编译文件
由于cocos2dx自带的spine运行库可能较老和spine版本不匹配,官方建议手动替换
The setup for cocos2d-x differs from most other Spine Runtimes because the cocos2d-x distribution includes a copy of the Spine Runtime files. This is not ideal because these files may be old and fail to work with the latest Spine editor. Also it means if cocos2d-x is updated, you may get newer Spine Runtime files which can break your application if you are not using the latest Spine editor. For these reasons, we have requested cocos2d-x to cease distributing the Spine Runtime files, but they continue to do so. The following instructions allow you to use the official Spine cocos2d-x runtime with your cocos2d-x project.
替换
- 首先删除 MyGame\cocos2d\cocos\editor-support\spine文件夹内所有内容
- 复制
spine-runtimes\spine-cpp\spine-cpp\include\spine
spine-runtimes\spine-cpp\spine-cpp\src\spine
spine-runtimes\spine-cocos2dx\src\spine
下所有内容至
MyGame\cocos2d\cocos\editor-support\spine- 接着在 MyGame\cocos2d\cocos\editor-support\spine 文件夹下新建文件
CMakeLists.txt 并写入以下内容
include_directories(editor-support)
file(GLOB_RECURSE COCOS_SPINE_SRC
${CMAKE_CURRENT_LIST_DIR}/*.cpp
${CMAKE_CURRENT_LIST_DIR}/**/*.cpp
)
file(GLOB_RECURSE COCOS_SPINE_HEADER
${CMAKE_CURRENT_LIST_DIR}/*.h
${CMAKE_CURRENT_LIST_DIR}/**/*.h
)
替换spine源码示例
- 复制 spine-runtimes\spine-cocos2dx\example\Classes 至 MyGame\Classes
- 复制 spine-runtimes\spine-cocos2dx\example\Resources\common 至 MyGame\Resources\common
配置运行环境
- 打开 /MyGame/proj.android/local.properties 文件
配置ndk路径
ndk.dir=..\sdk\ndk\20.0.5594570
由于使用的ndk版本较高所以还需要修改以下位置
- 修改 \MyGame\proj.android\gradle\wrapper\gradle-wrapper.properties
distributionUrl=https://services.gradle.org/distributions/gradle-5.4.1-all.zip- 修改 \MyGame\proj.android\build.gradle
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
}- 打开MyGame\CMakeLists.txt 找到
list(APPEND GAME_SOURCE
Classes/AppDelegate.cpp
Classes/HelloWorldScene.cpp
)
list(APPEND GAME_HEADER
Classes/AppDelegate.h
Classes/HelloWorldScene.h
)
修改为
list(APPEND GAME_SOURCE
Classes/AppDelegate.cpp
Classes/BatchingExample.cpp
Classes/CoinExample.cpp
Classes/GoblinsExample.cpp
Classes/RaptorExample.cpp
Classes/SkeletonRendererSeparatorExample.cpp
Classes/SpineboyExample.cpp
Classes/TankExample.cpp
)
list(APPEND GAME_HEADER
Classes/AppDelegate.h
Classes/AppMacros.h
Classes/BatchingExample.h
Classes/CoinExample.h
Classes/GoblinsExample.h
Classes/RaptorExample.h
Classes/SkeletonRendererSeparatorExample.h
Classes/SpineboyExample.h
Classes/TankExample.h
)
编译运行
使用Android Studio 打开 MyGame\proj.android
执行 sync project 等待编译完成,然后 run
或者使用命令编译运行,切换至项目根目录执行
cocos compile -p android -m release -j 2