cocos2d是OC写的,cocos2dx是c++写的
cocos2d只能在ios下运行,cocos2dx是跨平台的,ios和android平台都可以运行
cocos2d是外国人搞的,cocos2dx是中国人搞的。
cocos2dx是cocos2d的C++写法,但是游戏架构是一样的,都包含了精灵,导演,场景,动作等概念,他们是一脉相承的东西。你可以直接研究cocos2dx,没有什么障碍。虽然是有了cocos2d才有的cocos2dx,但是cocos2dx包含了cocos2d的主要思想,因此可以直接研究cocos2dx。
所以我们直接指导,结合Xcode搭建Cocos2d-X开发环境!
首先去Cocos2d-X官网下载安装:http://www.cocos2d-x.org/downloadcocos2dx下载地址
最新版本是:cocos2d-x-3.13.1
打开包,是这样的:
How to start a new game
1. Download the code from [cocos2d download site][4]
2. Run `setup.py`
3. Run the `cocos` script
Example:
$ cd cocos2d-x
$ ./setup.py
$ source FILE_TO_SAVE_SYSTEM_VARIABLE
$ cocos new MyGame -p com.your_company.mygame -l cpp -d NEW_PROJECTS_DIR
$ cd NEW_PROJECTS_DIR/MyGame
一.上面的Example的第一步是$ cd cocos2d-x,在终端里进入到你下载包的根目录下,我的是:cd /Users/apple/Desktop/cocos2d-x-3.13.1
二.第二步是:./setup.py 也就是运行该文件用来配置系统的一些环节变量
三.cocos new MyGame -p com.your_company.mygame -l cpp -d NEW_PROJECTS_DIR
3.1:MyGame:新建项目的名称,我的是:cocos2dxTest
com.your_company.mygame:iOS下项目的包名,我的是:com.aoqic.cocos2dxTest,也就是以后你的x-code里Bundle Identifier的名字,可以根据你们公司的情况填写
3.2:-l cpp:新建项目使用的编程语言 我的是:-l cpp
3.3:-d NEW_PROJECTS_DIR:保存项目的目录,我的是:-d /Users/apple/Desktop/cocos2dxTest,也就是你想把带有cocos2dx的x-code项目保存到你的电脑的哪一个文件夹下面
我的:
终端过程如下图,成功后打开你的桌面文件夹:/Users/apple/Desktop/cocos2dxTest会发现多了一个文件夹cocos2dxTest这是x-code创建工程下取得项目名称文件夹名字,打开/Users/apple/Desktop/cocos2dxTest/cocos2dxTest会多了上图黄色箭头指向区域的所有文件夹,这就是cocos2dx的内容,打开绿色箭头就可以打开x-code了。
我的x-code:
要选-moblie下的模拟器版本
command+r运行出现:
好了【iOS-cocos2d-X 游戏开发之一】在Mac下结合Xcode搭建Cocos2d-X开发环境!完成了!!!
学习一门新的语言要慢慢学,!!!加油💪
////////////////////////
2017-10-11统一回复更新一下,关于版本升级和安装失败的解决办法
$ cd cocos2d-x
$ ./setup.py
$ source FILE_TO_SAVE_SYSTEM_VARIABLE
$ cocos new MyGame -p com.your_company.mygame -l cpp -d NEW_PROJECTS_DIR
$ cd NEW_PROJECTS_DIR/MyGame
1.在官网下载最新版本的cocos2d-x-3.16后,打开终端进入cocos2d-x-3.16文件夹后执行./setup.py出现:IOError:[Errno 13] Permission denied '//.bash_profile'
1.1解决办法:是因为权限不够用sudo ./setup.py
./就是当前路径下 setup.py文件名字 连起来就是在当前路径下执行setup.py这个文件 但是如果这个文件没有执行权限是不能执行的若是提示Please enter the path of ANDROID_SDK_ROOT (or press Enter to skip):是让你按enter跳过不需要ANDROID_SDK_ROOT
2.接着出现:Please execute command: "source /Users/zhaotong/.bash_profile" to make added system variables take effect
sjhzdeMacBook-Air:cocos2d-x-3.16 zhaotong$ cocos new MyGame -p com.yjs.mygame -l cpp -d /Users/zhaotong/Desktop/1011cocosed-x
-bash: cocos: command not found
source /Users/zhaotong/.bash_profile 环境变量修改之后,需要执行source让其生效2.1解决办法:输入source /Users/zhaotong/.bash_profile
source /Users/zhaotong/.bash_profile终于搞好了,打开桌面的MyGame工程
3.发现编译时出现system报错,Call to unavailable function 'system': not available on iOS
Call to unavailable function 'system': not available on iOS3.1解决办法:
https://github.com/cocos2d/cocos2d-x/issues/17907
原因:XCode9 将system API删除,用 ntfw API替换,如下:
//在 #include <dirent.h>下方添加
#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
#include <ftw.h>
#endif
// 在 removeDirectory方法上方添加
namespace
{
#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
int unlink_cb(const char *fpath, const struct stat *sb, int typeflag,const struct stat *sb, int typeflag,
struct FTW *ftwbuf)
{
int rv = remove(fpath);
if (rv)
perror(fpath);
return rv;
}
#endif
}
//将 removeDirectory方法替换为 :
bool FileUtils::removeDirectory(const std::string& path){
#if !defined(CC_TARGET_OS_TVOS)
#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
if (nftw(path.c_str(), unlink_cb, 64, FTW_DEPTH | FTW_PHYS) == -1)
return false;
else
return true;
#else
std::string command = "rm -r ";
// Path may include space.
command += "\"" + path + "\"";
if (system(command.c_str()) >= 0)
return true;
else
return false;
#endif // (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
#else
return false;
#endif // !defined(CC_TARGET_OS_TVOS)
}
最终运行成功:
修改demo:https://github.com/mrzhao12/cocos2d-X
好了【iOS-cocos2d-X 游戏开发之一】在Mac下结合Xcode搭建Cocos2d-X开发环境!完成了!!!
学习一门新的语言要慢慢学,!!!加油💪
网友评论
Setting up cocos2d-x...
->Check environment variable COCOS_CONSOLE_ROOT
->Search for environment variable COCOS_CONSOLE_ROOT...
->COCOS_CONSOLE_ROOT is found : /Users/gaowei/Downloads/cocos2d-x-3.15.1/tools/cocos2d-console/bin
->Check environment variable COCOS_X_ROOT
->Search for environment variable COCOS_X_ROOT...
->COCOS_X_ROOT is found : /Users/gaowei/Downloads
->Check environment variable COCOS_TEMPLATES_ROOT
->Search for environment variable COCOS_TEMPLATES_ROOT...
->COCOS_TEMPLATES_ROOT is found : /Users/gaowei/Downloads/cocos2d-x-3.15.1/templates
->Configuration for Android platform only, you can also skip and manually edit "/Users/gaowei/.bash_profile"
->Check environment variable NDK_ROOT
->Search for environment variable NDK_ROOT...
->NDK_ROOT is found : /Users/gaowei/Downloads/adt-bundle-mac-x86_64-20131030/sdk/ndk-bundle
->Check environment variable ANDROID_SDK_ROOT
->Search for environment variable ANDROID_SDK_ROOT...
->ANDROID_SDK_ROOT not found
->Search for command android in system...
->Command android not found
->Please enter the path of ANDROID_SDK_ROOT (or press Enter to skip):
->Check environment variable ANT_ROOT
->Search for environment variable ANT_ROOT...
->ANT_ROOT not found
->Search for command ant in system...
->Command ant not found
->Please enter the path of ANT_ROOT (or press Enter to skip):
Please execute command: "source /Users/gaowei/.bash_profile" to make added system variables take effect
gaoweideMacBook-Pro:cocos2d-x-3.15.1 gaowei$ cocos new cocos2dxTest -p com.hjt.cocos2dxTest -l cpp -d NEW_PROJECTS_DIR
-bash: cocos: command not found
gaoweideMacBook-Pro:cocos2d-x-3.15.1 gaowei$
为啥我的是这样的呢?创建不了项目啊
->Check environment variable COCOS_CONSOLE_ROOT
->Search for environment variable COCOS_CONSOLE_ROOT...
->COCOS_CONSOLE_ROOT is found : /Users/cihuiyu/Desktop/cocos2d-x-3.13/tools/cocos2d-console/bin
->Check environment variable COCOS_X_ROOT
->Search for environment variable COCOS_X_ROOT...
->COCOS_X_ROOT is found : /Users/cihuiyu/Desktop
->Check environment variable COCOS_TEMPLATES_ROOT
->Search for environment variable COCOS_TEMPLATES_ROOT...
->COCOS_TEMPLATES_ROOT is found : /Users/cihuiyu/Desktop/cocos2d-x-3.13/templates
:->Configuration for Android platform only, you can also skip and manually edit "/Users/cihuiyu/.bash_profile"
->Check environment variable NDK_ROOT
->Search for environment variable NDK_ROOT...
->NDK_ROOT not found
->Search for command ndk-build in system...
->Command ndk-build not found
->Please enter the path of NDK_ROOT (or press Enter to skip):
知道是什么原因吗?