1、引擎代码的下载
官方版本地址为:
http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Download
目前最新版本是cocos2d-2.0-rc0a-x-2.0
官方代码库:https://github.com/cocos2d/cocos2d-x/(想尝鲜或想把自己代码提交给cocos2d-x的用户,还可以从https://github.com/cocos2d/cocos2d-x.git fork一个工作流,来进行自己的创造性活动,不过都是用git工具来操作的哦)
2、各平台环境搭建
在此以cocos2d-2.0-rc0a-x-2.0版本为例,下载完成后,把其解压到任一路径都有,在此我以我电脑windows 7 旗舰版的环境讲解,存放于G:\cocos2d-x(注意此目录不能有空格,如果有,在后面创建目录工程时,会创建失败的) 目录底下,解压后的目录结构和文件截图如下:
2.2.1 在win32下编译 cocos2d-x项目
为了生成windows下需要的lib、dll文件,我们需要将解压后的cocos2d-x项目在win32下编译一次,我这里是用的是visual studio 2010.
1、双击cocos2d-win32.vc2010.sln,将整个cocos2d-x项目打开
2、右键点击解决方案,将整个项目重新生成(或直接按F6 键),如下如图
3、如果一切顺利的话,正常会在G:\cocos2d-x\Debug.win32 目录下生成如下图目录:
4、可以看到此目录下已经编译成功cocos2d-x项目几大项目和自带的一些demo。
我们可以将试着运行下已生成的HelloWorld.exe文件,发现出现找不到资源文件的错误:
可以将G:\cocos2d-x\Debug.win32\HelloWorld\Resources 目录下的所有文件,都复制到G:\cocos2d-x\Debug.win32目录下,再次双击运行发现不报错,但屏幕没任何输出,且任务管理器已经有这个进程,于是怀疑是windows 7上兼容性的问题,可以试下将HelloWorld.exe设置为在xp sp3模式下运行(点击右键属性,选择兼容性)
然后将任务管理器里的HelloWorld.exe进程杀掉,再次运行,终于运行成功HelloWord项目的了(对另外几个输出的exe项目也进行类似的设置,则windows 7环境运行没问题)
5、下来我们为自己真正创建属于自己的cocos2d-x的win 32项目
首先我们给vs2010安装工程模板,但默认的工程模板里面写的都是相对路径,故我们需要修改下cocos2d-x的vs2010的工程模板,用文本编辑器打开G:\cocos2d-x\template\msvc\CCAppWiz.win32\Scripts\1033目录下的default.js,找到AddConfigurations函数,发现默认配置的包含文件夹路径都是相对路径:
// 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 = "http://www.cnblogs.com/http://www.cnblogs.com/../lua";//这个也要修改下路径 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-win 32 项目
可以看到默认是选中Box2D物理引擎和Cocos Denshion 音频支持的,我们可以选中默认的,点击Finish。
8、检查HelloCocos2d的项目属性:
右键下HelloCocos2d项目=>属性
查看包含目录:发现都指向我们之前修改的模板的文件指向的绝对路径了。至此工程模板安装成功。
9、按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目录下)。
重新调试编译下,发现编译成功,但是还是没有输出任何窗口,还是windows 7兼容性问题,可以参考步骤4的设置方法,设置HelloCocos2d\Debug.win32的HelloCocos2d.win32.exe文件属性即可,在F5次调试运行,终于成功了: