注: 从个人博客园移植而来
(1) 使用终端命令打开**.bash_profile**文件, 命令:
open .bash_profile(Enter)
打开文件后,分别配置android sdk, ant, ndk等环境变量,我的在 Android目录下,故配置如下(针对于目录,仅供参考而已):
# sdk
export ANDROID_SDK_ROOT=Android/android-sdk-macosx
export PATH=$PATH:$ANDROID_SDK_ROOT
export PATH=$PATH:$ANDROID_SDK_ROOT/platform-tools
# ndk
export NDK_ROOT=Android/android-ndk-r9d
export PATH=$PATH:$NDK_ROOT
# ant
export ANT_ROOT=Android/apache-ant-1.9.5
export PATH=${PATH}:${ANT_ROOT}/bin
(2) 使用终端,进入cocos2d-x 3.8 目录下,再输入命令:
./setup.py(Enter)
会出现类似如下所示:
Setting up cocos2d-x...
->Check environment variable COCOS_CONSOLE_ROOT
->Search for environment variable COCOS_CONSOLE_ROOT...
->COCOS_CONSOLE_ROOT not found
-> Add COCOS_CONSOLE_ROOT environment variable...
->Added COCOS_CONSOLE_ROOT=cocos2d-x-3.8/tools/cocos2d-console/bin
->Check environment variable COCOS_X_ROOT
->Search for environment variable COCOS_X_ROOT...
->COCOS_X_ROOT not found
-> Add COCOS_X_ROOT environment variable...
->Added COCOS_X_ROOT=Documents
->Check environment variable COCOS_TEMPLATES_ROOT
->Search for environment variable COCOS_TEMPLATES_ROOT...
->COCOS_TEMPLATES_ROOT not found
-> Add COCOS_TEMPLATES_ROOT environment variable...
->Added COCOS_TEMPLATES_ROOT=cocos2d-x-3.8/templates
->Configuration for Android platform only, you can also skip and manually edit "/User/用户名/.bash_profile"
->Check environment variable NDK_ROOT
->Search for environment variable NDK_ROOT...
->NDK_ROOT is found : Android/android-ndk-r9d
->Check environment variable ANDROID_SDK_ROOT
->Search for environment variable ANDROID_SDK_ROOT...
->ANDROID_SDK_ROOT is found : Android/android-sdk-macosx
->Check environment variable ANT_ROOT
->Search for environment variable ANT_ROOT...
->ANT_ROOT is found : Android/apache-ant-1.9.5
A backup file "/Users/用户名/.bash_profile.backup1" is created for "/Users/用户名/.bash_profile".
Please execute command: "source /Users/用户名/.bash_profile" to make added system variables take effect
使用setup.py其实质是在你的.bash_profile为你配置了COCOS_CONSOLE_ROOT,COCOS_X_ROOT,COCOS_TEMPLATES_ROOT的路径,你可以再次查看你的.bash_profile文件查看区别,如下:
# Add environment variable COCOS_CONSOLE_ROOT for cocos2d-x
export COCOS_CONSOLE_ROOT=/Users/用户名/Documents/cocos2d-x-3.8/tools/cocos2d-console/bin
export PATH=$COCOS_CONSOLE_ROOT:$PATH
# Add environment variable COCOS_X_ROOT for cocos2d-x
export COCOS_X_ROOT=/Users/用户名/Documents
export PATH=$COCOS_X_ROOT:$PATH
# Add environment variable COCOS_TEMPLATES_ROOT for cocos2d-x
export COCOS_TEMPLATES_ROOT=/Users/用户名/Documents/cocos2d-x-3.8/templates
export PATH=$COCOS_TEMPLATES_ROOT:$PATH
(3) 然后打开新的终端命令,输入命令:
cocos new(Enter)
出现如下所示,表示,配置OK了。
usage: cocos new [-h] [-p PACKAGE_NAME] [-d DIRECTORY] [-t TEMPLATE_NAME]
[--ios-bundleid IOS_BUNDLEID] [--mac-bundleid MAC_BUNDLEID]
[-e ENGINE_PATH] [--portrait] [--no-native]
(-l {cpp,lua,js} | --list-templates | -k TEMPLATE_NAME)
[PROJECT_NAME]
cocos new: error: one of the arguments -l/--language --list-templates -k/--template-name is required
(4) 创建示例项目,可以输入这样的命令:
--[[
NewVersion: 代表你的工程名
-p: com.demo.org 代表你的包名
-l: 代表你使用的语言,支持cpp, lua, js
-d: 代表你的项目路径
]]
cocos new NewProject -p com.demo.org -l cpp -d /Users/用户名/Documents/cocos2d-x-3.8/projects
-- 不考虑包名什么的,可以这样:
cocos new NewProject -l lua
执行的过程中,如果出现类似以下所示,表示成功
> 拷贝模板到 /Users/用户名/Documents/cocos2d-x-3.8/projects/VesionDemo
> 拷贝 cocos2d-x ...
> 替换文件名中的工程名称,'HelloCpp' 替换为 'NewVesion'。
> 替换文件中的工程名称,'HelloCpp' 替换为 'NewVesionDemo'。
> 替换工程的包名,'org.cocos2dx.hellocpp' 替换为 'com.demo.org'。
> 替换 Mac 工程的 Bundle ID,'org.cocos2dx.hellocpp' 替换为 'com.demo.org'。
> 替换 iOS 工程的 Bundle ID,'org.cocos2dx.hellocpp' 替换为 'com.demo.org'。
大概的流程就是如此,希望对大家有所帮助,感谢!