首先工具列表
cocos2d-x(引擎)
vs 2010 (C++开发)
cygwin(linux环境 由于网络版本太大,用的是本地安装版本)
android SDK NDK
步骤:(注意我的操作系统为win7)
1.下载cocos2d-x 官方地址为:http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Download
2.下载下来解压存放于一个目录中(注意此目录不能有空格,否则后面创建目录工程时,会创建失败的)
3.在win32下编译 cocos2d-x
为了生成windows下需要的lib、dll文件,我们需要编译一次,cocos2d-x下有vs2010的解决方案,直接打开然后对整个项目重新生成,如果顺利的话,在Debug.win32目录会有生成一推文件,有些是demo。
4.可以看到此目录下已经编译成功的coco2d-x项目,可以运行一些exe文件,如果打开没有资源,只需要复制资源到Debug.win32下就好
5.下来我们为自己真正创建属于自己的cocos2d-x的win 32项目
// Additional Inlcude Directories
var strAddIncludeDir = '.;..\\Classes';
strAddIncludeDir = ';..\\..\\cocos2dx';
strAddIncludeDir = ';..\\..\\cocos2dx\\actions';
strAddIncludeDir = ';..\\..\\cocos2dx\\base_nodes';
strAddIncludeDir = ';..\\..\\cocos2dx\\cocoa';
strAddIncludeDir = ';..\\..\\cocos2dx\\effects';
strAddIncludeDir = ';..\\..\\cocos2dx\\include';
strAddIncludeDir = ';..\\..\\cocos2dx\\kazmath\\include';
strAddIncludeDir = ';..\\..\\cocos2dx\\keypad_dispatcher';
strAddIncludeDir = ';..\\..\\cocos2dx\\label_nodes';
strAddIncludeDir = ';..\\..\\cocos2dx\\layers_scenes_transitions_nodes';
strAddIncludeDir = ';..\\..\\cocos2dx\\menu_nodes';
strAddIncludeDir = ';..\\..\\cocos2dx\\misc_nodes';
strAddIncludeDir = ';..\\..\\cocos2dx\\particle_nodes';
strAddIncludeDir = ';..\\..\\cocos2dx\\script_support';
strAddIncludeDir = ';..\\..\\cocos2dx\\shaders';
strAddIncludeDir = ';..\\..\\cocos2dx\\sprite_nodes';
strAddIncludeDir = ';..\\..\\cocos2dx\\support';
strAddIncludeDir = ';..\\..\\cocos2dx\\text_input_node';
strAddIncludeDir = ';..\\..\\cocos2dx\\textures';
strAddIncludeDir = ';..\\..\\cocos2dx\\tileMap_parallax_nodes';
strAddIncludeDir = ';..\\..\\cocos2dx\\touch_dispatcher';
strAddIncludeDir = ';..\\..\\cocos2dx\\platform';
strAddIncludeDir = ';..\\..\\cocos2dx\\platform\\win32';
strAddIncludeDir = ';..\\..\\cocos2dx\\platform\\third_party\\win32';
strAddIncludeDir = ';..\\..\\cocos2dx\\platform\\third_party\\win32\\OGLES';
if (wizard.FindSymbol('CC_USE_BOX2D')) {
strAddIncludeDir = ';..\\..\\';
}
if (wizard.FindSymbol('CC_USE_CHIPMUNK')) {
strAddIncludeDir = ';..\\..\\chipmunk\\include\\chipmunk';
}
if (wizard.FindSymbol('CC_USE_COCOS_DENSHION_SIMPLE_AUDIO_ENGINE')) {
strAddIncludeDir = ';..\\..\\CocosDenshion\\Include';
}
if (wizard.FindSymbol('CC_USE_LUA')) {
strAddIncludeDir = ';..\\..\\lua\\cocos2dx_support';
strAddIncludeDir = ';..\\..\\lua\\tolua';
strAddIncludeDir = ';..\\..\\lua\\lua';
function GetTargetName(strName, strProjectName) {
try {
var strTarget = strName;
var nIndex = strName.indexOf("root");
if (nIndex >= 0) {
strTarget = strName.substring(0, nIndex) strProjectName strName.substring(nIndex 4, strName.length);
}
var strTemp = "";//这个也要修改下路径
nIndex = strTarget.indexOf(strTemp);
if (nIndex >= 0) {
strTarget = "Classes" strTarget.substring(nIndex strTemp.length, strTarget.length);
}
return strTarget;
}
catch (e) {
throw e;
}
}
即如果用默认模板安装的话,必须使用自带的解决方案,在此基础上添加工程的方法创建新的项目,但如果我们要经常做很多不关的cocos2d-x项目,这样后面岂不是要在一个解决方案管理或者复制粘贴多次原来的解决方案,这明显是不合理的,所以可以见此脚本中的相对路径的地方都改为我们cocos2d-x源码存放的绝对路径。主要有以下几处:
// Additional Inlcude Directories
var strAddIncludeDir = '.;..\\Classes';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\actions';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\base_nodes';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\cocoa';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\effects';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\include';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\kazmath\\include';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\keypad_dispatcher';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\label_nodes';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\layers_scenes_transitions_nodes';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\menu_nodes';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\misc_nodes';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\particle_nodes';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\script_support';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\shaders';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\sprite_nodes';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\support';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\text_input_node';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\textures';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\tileMap_parallax_nodes';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\touch_dispatcher';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\platform';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\platform\\win32';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\platform\\third_party\\win32';
strAddIncludeDir = ';G:\\Cocos2d-x\\cocos2dx\\platform\\third_party\\win32\\OGLES';
if (wizard.FindSymbol('CC_USE_BOX2D')) {
strAddIncludeDir = ';G:\\Cocos2d-x\\';
}
if (wizard.FindSymbol('CC_USE_CHIPMUNK')) {
strAddIncludeDir = ';G:\\Cocos2d-x\\chipmunk\\include\\chipmunk';
}
if (wizard.FindSymbol('CC_USE_COCOS_DENSHION_SIMPLE_AUDIO_ENGINE')) {
strAddIncludeDir = ';G:\\Cocos2d-x\\CocosDenshion\\Include';
}
if (wizard.FindSymbol('CC_USE_LUA')) {
strAddIncludeDir = ';G:\\Cocos2d-x\\lua\\cocos2dx_support';
strAddIncludeDir = ';G:\\Cocos2d-x\\lua\\tolua';
strAddIncludeDir = ';G:\\Cocos2d-x\\lua\\lua';
}
function GetTargetName(strName, strProjectName) {
try {
var strTarget = strName;
var nIndex = strName.indexOf("root");
if (nIndex >= 0) {
strTarget = strName.substring(0, nIndex) strProjectName strName.substring(nIndex 4, strName.length);
}
var strTemp = "G:/Cocos2d-x/lua/lua";
nIndex = strTarget.indexOf(strTemp);
if (nIndex >= 0) {
strTarget = "Classes" strTarget.substring(nIndex strTemp.length, strTarget.length);
}
return strTarget;
}
catch (e) {
throw e;
}
}
修改完毕,给vs 2010安装工程模板,双击G:\cocos2d-x目录下的install-templates-msvc.bat文件,会执行一些bat处理命令,windows 7环境中途会出现一写权限控制的对话框,选择Y即可,这样以后每次cocos2d-x版本升级,我们直接替换G:\cocos2d-x目录下的文件,并重新编译即可。
6、打开vs 2010,新建项目如果可以出现以下选项,说明工程以安装到vs2010了
7.(注意很重要)创建cocos2d-x项目之后我们f5运行发现报错:错误 3 error LNK1104: 无法打开文件“libcocos2d.lib” G:\MyWork\Cocos2d-x application\HelloCocos2d\HelloCocos2d\proj.win32\LINK HelloCocos2d.win32
即我们没将之前编译cocos2d-x项目生产的Lib包没放到HelloCocos2d项目下,将
G:\cocos2d-x\Debug.win32 下所有.lib和dll文件复制到HelloCocos2d\Debug.win32目录下即可(Tips:为了后续再次创建cocos2d-x win32项目都复制这些dll文件到项目路径下,可以将其全部复制到系统的system32目录下)。
8.用cocos2d-x目录下的create-android-project.bat创建项目, 包名及项目名自己定义 然后选择你机器中存在的2.0以上的sdk,这样项目应该就会生成了,在cocos2d-x下以项目名为目录。打开之后里边应该有三个目录,其中的Classes为C++代码,Resources中为游戏需要的资源,proj.android目录就是我们的项目。
================================================
下面说下如何交叉编译android项目
1.create-android-project.bat下的 _CYGBIN _ANDROIDTOOLS 及_NDKROOT分别对应于cygwin\bin android-sdk\tools 和ndk目录 修改完成保存。包名及项目名自己定义 然后选择你机器中存在的2.0以上的sdk,这样项目应该就会生成了,在cocos2d-x下以项目名为目录。如何你编辑好游戏了,用你自己的classes文件盒resouces文件替换掉他们。
进入jni文件中的Android.mk中添加你自己项目的.cpp文件
2.然后用Cygwin进入到项目目录,注意这里的build_native.sh文件,运行它 如果提示NDK目录没指定,编辑这个文件,设置NDK变量为本机NDK目录 这样再运行就可以跑了,但是如果是win7的话还要注意权限的问题,最好cygwin运行的时候以管理员身份运行。如果编译成功的话会注册一个libgame.so文件,这样就可以运行项目到android手机上了。
注意 cocos2dx交叉编译android时出现make: *** [obj/local/armeabi bgame.so] Error 1 可能更新下NDK就好了
到此算是能够搭建起android游戏了,以后注意力会放在cocos2d-x游戏的开发,多数都是C++的东西