游戏开发教程cocos2d-x移植之一

前言

知易的cocos2d-iphone游戏开发教程帮助不少人开始了游戏开发之旅,我们由衷地感谢他。这套教程已经发表了一年多时间,在这段时间里,随着cocos2d-iphone的升级,接口发生了不少变动。在我们这些用惯了C++的人眼里,ObjC的代码看上去总是怪怪的。所以我将知易的教程向我使用的cocos2d-1.0.1-x-0.11.0做了一次移植,希望对学习cocos2d-x的朋友有所帮助。

谁也不知道cocos2d-x的接口何时会发生变动,如果本教程的代码在你使用的版本上遇到问题,你可以到<wbr><a href="http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Upgrade_Guides" target="_blank">http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Upgrade_Guides</a><wbr>查看升级指南。</wbr></wbr>

本文说不上是原创,我只是将知易教程中的cocos2d-iphone代码替换成当前可用的cocos2d-x代码,就算是移植吧。我尽量保持原文结构,望知易不要介意。

安装cocos2d-x

无论是什么引擎或者什么库,我都推荐大家从最新的稳定版本开始入手。这个方法几乎总是对的。访问<wbr><a href="http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Download" target="_blank">http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Download</a><wbr>页面来获取最新的cocos2d-x引擎。<br style="margin:0px; padding:0px"><em style="margin:0px; padding:0px"><strong style="margin:0px; padding:0px">我使用的是Microsoft Visual C++ 2010 学习版以及cocos2d-1.0.1-x-0.11.0 这一点以后就不再重复说明了。</strong></em><br style="margin:0px; padding:0px"><em style="margin:0px; padding:0px"><a href="http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Chapter_1_-_How_to_Create_a_New_cocos2d-x_project_on_multi-platforms" target="_blank">官方教程</a>中是通过 build-win32.bat 编译整个引擎,但运行批处理可能会被个别病毒防御软件拦截,我们这里直接通过VC的IDE环境编译。</em><br style="margin:0px; padding:0px"> 将下载的文件解压缩到任意目录,双击对应的解决方案(我这里是cocos2d-win32.vc2010.sln)启动VC开发环境。然后生成解决方案,对整个工程进行编译。</wbr></wbr>

待编译完成后,将HelloWorld设为启动项目,运行。

对于那些小倒霉蛋儿,如果演示示例在你的电脑上不能正常运行,你可以访问<wbr><a href="http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Chapter_1_-_How_to_Create_a_New_cocos2d-x_project_on_multi-platforms#15-Portal-for-unlucky-guys" target="_blank">http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Chapter_1_-_How_to_Create_a_New_cocos2d-x_project_on_multi-platforms#15-Portal-for-unlucky-guys</a><wbr>来寻求帮助。</wbr></wbr>

创建你的第一个cocos2d-x应用

完全手动为项目配置cocos2d-x是非常繁琐的,好在开发人员为我们提供了项目模板,你只需要轻轻双击install-templates-msvc.bat就可以将他安装到VC中去。有了这个神兵利器我们就可以开始神奇的cocos2d-x之旅了。
右键单击“解决方案'cocos2d-win32.vc2010'”,选择“添加 -> 新建项目”


然后在“已安装的模板”内选择“Cocos2d-x -> Cocos2d-win32 Application”,并填写名称“ZYG001”。存储位置使用默认的,不要改变,否则模板配置的项目路径就失效了。


单击“确定 -> 下一步”,来到cocos2d-x的特性选择页面。


根据你的实际需要选择包含哪些引擎特性。在这里我们只是想要做个简单的演示,物理引擎和声音引擎都是不需要的,所以我们将默认的对勾去掉。

当你点击Finish按钮之后,向导会自动帮你生成一个简单的cocos2d-x应用。

虽然他是简单的,但他是完整的,你只需编译一下,然后就可以运行,并查看结果了。

对cocos2d-x项目的初步分析

根据上面的截图可以看出,项目默认分Classes Resources win32三个文件夹。
Classes下存放的是程序的主要源代码,
Resources下放的是资源文件,
win32目录下的文件是平台相关的,99%的情况下不需求变动。
1)主程序入口
打开main.cpp可以看到如下代码

 1 #include "main.h"
2
3 #include "AppDelegate.h"
4
5 int APIENTRY _tWinMain(HINSTANCE hInstance,
6 HINSTANCE hPrevInstance,
7 LPTSTR lpCmdLine,
8 int nCmdShow)
9 {
10 UNREFERENCED_PARAMETER(hPrevInstance);
11 UNREFERENCED_PARAMETER(lpCmdLine);
12
13 // create the application instance
14 AppDelegate app;
15
16 return cocos2d::CCApplication::sharedApplication().run();
17 }

看起来是不是有点儿像MFC的CWinApp呢?
2)AppDelegate的使用
实际上AppDelegate是从CCApplication派生出来的。

作为初学者,目前只需要注意1个成员函数,那就是applicationDidFinishLaun<wbr>ching。他负责初始化CCDirector并创建第1个CCScene供程序显示。</wbr>

 1 bool AppDelegate::applicationDidFinishLaun<wbr>ching()<br style="margin:0px; padding:0px"><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">2</span> {<br style="margin:0px; padding:0px"><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">3</span> <span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5">//</span><span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5"> initialize director</span><span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5"><br style="margin:0px; padding:0px"></span><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">4</span> CCDirector *pDirector = CCDirector::sharedDirector();<br style="margin:0px; padding:0px"><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">5</span> pDirector-&gt;setOpenGLView(&amp;CCEGLView::sharedOpenGLView());<br style="margin:0px; padding:0px"><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">6</span> <br style="margin:0px; padding:0px"><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">7</span> <span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5">//</span><span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5"> enable High Resource Mode(2x, such as iphone4) and maintains low resource on other devices.<br style="margin:0px; padding:0px"></span><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">8</span> <span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5">// </span><span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5">pDirector-&gt;enableRetinaDisplay(true);<br style="margin:0px; padding:0px"></span><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">9</span> <span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5"><br style="margin:0px; padding:0px"></span><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">10</span> <span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5">//</span><span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5"> turn on display FPS</span><span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5"><br style="margin:0px; padding:0px"></span><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">11</span> pDirector-&gt;setDisplayFPS(<span style="font-family:'Courier new'; font-size:12px; color:#00ff; margin:0px; padding:0px; line-height:1.5">true</span>);<br style="margin:0px; padding:0px"><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">12</span> <br style="margin:0px; padding:0px"><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">13</span> <span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5">//</span><span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5"> set FPS. the default value is 1.0/60 if you don't call this</span><span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5"><br style="margin:0px; padding:0px"></span><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">14</span> pDirector-&gt;setAnimationInterval(<span style="font-family:'Courier new'; font-size:12px; color:#80080; margin:0px; padding:0px; line-height:1.5">1.0</span> / <span style="font-family:'Courier new'; font-size:12px; color:#80080; margin:0px; padding:0px; line-height:1.5">60</span>);<br style="margin:0px; padding:0px"><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">15</span> <br style="margin:0px; padding:0px"><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">16</span> <span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5">//</span><span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5"> create a scene. it's an autorelease object</span><span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5"><br style="margin:0px; padding:0px"></span><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">17</span> CCScene *pScene = HelloWorld::scene();<br style="margin:0px; padding:0px"><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">18</span> <br style="margin:0px; padding:0px"><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">19</span> <span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5">//</span><span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5"> run</span><span style="font-family:'Courier new'; font-size:12px; color:#0800; margin:0px; padding:0px; line-height:1.5"><br style="margin:0px; padding:0px"></span><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">20</span> pDirector-&gt;runWithScene(pScene);<br style="margin:0px; padding:0px"><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">21</span> <span style="font-family:'Courier new'; font-size:12px; color:#00ff; margin:0px; padding:0px; line-height:1.5">return</span> <span style="font-family:'Courier new'; font-size:12px; color:#00ff; margin:0px; padding:0px; line-height:1.5">true</span>;<br style="margin:0px; padding:0px"><span style="font-family:'Courier new'; font-size:12px; color:#08080; margin:0px; padding:0px; line-height:1.5">22</span> }</wbr>

小结

通过本章内容,我们对cocos2d-x引擎有了一个初步的体验,从下一章开始,我们将详细讲解该引擎的内部特性和功能。为了让大家可以从掌握全局架构逐步深入到熟练细节,我们将从游戏编程的基本概念开始逐步学习。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值