本篇基于
一.環(huán)境配置
下載cocos2d
git clone https://github.com/cocos2d/cocos2d-x.git
cd cocos2d-x
git checkout cocos2d-x-3.17.1
git submodule update --init
克隆存儲(chǔ)庫(kù)后涩哟,執(zhí)行download-deps.py下載并安裝依賴(lài)項(xiàng)心例。
python download-deps.py
接著執(zhí)行
python setup.py
配置環(huán)境變量,這將會(huì)配置cocos運(yùn)行環(huán)境
驗(yàn)證cocos
打開(kāi)cmd命令 執(zhí)行cocos -v 查看環(huán)境是否配置成功沮趣,顯示如下則說(shuō)明配置成功
克隆spine
git clone https://github.com/EsotericSoftware/spine-runtimes.git
cd spine-runtimes
git checkout 3.7
git submodule update --init
二.創(chuàng)建項(xiàng)目
使用如下命令創(chuàng)建cocos項(xiàng)目
cocos new 項(xiàng)目名稱(chēng) -p (iOS bundle id或Android包名) -l cpp -d 存儲(chǔ)位置路徑
等待項(xiàng)目創(chuàng)建成功
替換編譯文件
由于cocos2dx自帶的spine運(yùn)行庫(kù)可能較老和spine版本不匹配粹庞,官方建議手動(dòng)替換
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文件夾內(nèi)所有內(nèi)容
- 復(fù)制
spine-runtimes\spine-cpp\spine-cpp\include\spine
spine-runtimes\spine-cpp\spine-cpp\src\spine
spine-runtimes\spine-cocos2dx\src\spine
下所有內(nèi)容至
MyGame\cocos2d\cocos\editor-support\spine- 接著在 MyGame\cocos2d\cocos\editor-support\spine 文件夾下新建文件
CMakeLists.txt 并寫(xiě)入以下內(nèi)容
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源碼示例
- 復(fù)制 spine-runtimes\spine-cocos2dx\example\Classes 至 MyGame\Classes
- 復(fù)制 spine-runtimes\spine-cocos2dx\example\Resources\common 至 MyGame\Resources\common
配置運(yùn)行環(huán)境
- 打開(kāi) /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'
}- 打開(kāi)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
)
編譯運(yùn)行
使用Android Studio 打開(kāi) MyGame\proj.android
執(zhí)行 sync project 等待編譯完成玫芦,然后 run
或者使用命令編譯運(yùn)行耻瑟,切換至項(xiàng)目根目錄執(zhí)行
cocos compile -p android -m release -j 2