【Cocos2d-x】实现资源热更新

http://www.cnblogs.com/tuituier/articles/3859501.html 这也是一篇热更新的文章

cocos-x 热更新探索

来到园子这么久了,竟然连博客写博客的权限才开通,相当的汗颜啊,之前在百度空间写的博客,发现还是术业有专攻最好,程序员还是老老实实呆在程序员的圈子里吧!对了,之前百度空间的地址顺便宣传下,哈哈:

http://hi.baidu.com/awming520

---------------------------------正文分割线------------------------------------

本文是探索cocos-x热更新,其实就是探索cocos-lua或者js的脚本和资源热更新,热更的作用也不用我在这里为大家普及了。

文中主要使用python作为服务器的脚本,lua作为客户端脚本,是用cocos code ide作为开发环境。

在此之前可以参考我之前发表在cocoachina上关于热更新后台的实现,里面大概讲解了实现与原理(文件结构有小小变动,现在增加了项目名称,可以发布多个项目):

http://www.cocoachina.com/bbs/read.php?tid=213054

后台环境:
python 2.7 + webpy0.37
webpy安装:
1、解压webpy,cmd至webpy路径下
2、cmd运行python setup.py install
详细了解webpy,请访问其官网:http://webpy.org/


为了保持客户端最安全与最小化热更新,我们需要判断出每个版本的差异,将差异文件下载到客户端。为了差异识别,我们将生产res与src下所有文件的MD5,并将其保存至相关文件夹,保存内容如下:
src/test1.lua:e6e8feb29a41a0d83c29c834c10a42a2
src/test2.lua:b87957f382a4a292db91bd937b66a000
res/ani/enemy/monster.csb:6a42885d985f777418d97ef956758f2e
res/ani/enemy/monster.exportjson:1c26ec5818281794e98a4d05c3d871c4
后面我们只需要对比客户端当前版本与服务器最新版本的资源列表的md5以及判断哪些是新增文件,这样我们就可最小化获取需要更新的列表。
到此,我们可以将此文件连同当前版本文件放至服务器,让客户端下载更新列表,并在客户端判断需要下载那些文件,但是这样真的好么,如果一次更新n个文件,那样的连接将会有n次,可能是我多虑了。但我考虑到我们的版本不可能无限次发布,因此我决定将版本差异的包放在服务器,这样客户端只用告诉我们他当前什么版本,就可以获取到升级至最新版本的更新包。
比如客户端当前版本为1.0,服务器最新版本为2.0,通过url传至服务器,客户端就可以获取一个1.0升级到2.0的资源包,客户端通过assertManager可以文件解压,我们再将解压目录文件移动至我们的查找目录就ok了。
服务器通过各个版本的资源列表,获取改动和新增的文件,并将其打包压缩至zip文件。而且由于我们发布的版本是确定的,因此这些更新文件在服务器只会生成一次,对服务器来说基本没什么压力,而且我们可以将其预先生成,保存至服务器。

在服务端,我采用如下路径配置:
App:(项目名称)
--project:(保存最新项目资源,手工配置)
----res:(资源路径)
----src:(脚本路径)
--publish:(更新包路径,目录下文件自动生成)
----version.txt(当前最新版本号,手工修改)
----1.0(1.0版本包)
------list.txt(当前版本1.0的资源列表及MD5)
------update.zip(只有基本版本才会在当前路径层级下有更新包)
----2.0
------list.txt
------1.0
--------update.zip(1.0升级到2.0需要的更新资源包)
...
我们需要手工配置下project文件,这里面保存我们所发布的最新资源和脚本;publish下保存的是各种版本的更新包。
最终我们运行程序后,可以通过url访问版本与获取更新资源包:
获取最新版本号:http://127.0.0.1:8080/?action=ver
下载更新包:http://127.0.0.1:8080/?action=update.zip&ver=1.1(ver为本地当前版本,url中需要有.zip,cocos2d-x终对url做了判断)


下载地址:http://pan.baidu.com/s/1eQ7WJxK
其余信息可以阅读readme.txt

简单说下思路就是,将客户端当前版本与服务器最新版本做md5比对,将最新版本与客户端版本差异的文件打包,并下载至客户端,完成资源的热更新。

这种后台实现主要解决一下几个问题:

1.如果将资源包完全打包为一个文件,需要下载的文件势必会大很多,每次都会下载很多相同的文体。

2.如果按照版本差异更新,则需要将最新版本的资源包挨个下载一遍,多麻烦呀!

3.如果对服务器文件作md5记录,而单独下载相关文件。这样将会造成复杂的客户端代码,而且每个需要更新的文件都去连接服务器,给服务器造成了压力,而且包的传输肯定要比单个小文件来的快,而且更不容易出错吧!(这完全是个人猜想)

4.第三条也说了,我想让客户端代码变得简单。最简单的方式就是不写或者少些代码,我就想是用cocos-x自带的assertmanager来实现热更新。(其实我是偷懒而已)

说说我认为这样做的好处吧:

1.需要更新的资源减少了。

2.服务器需要保存的版本减少了,只需要保存最新两个版本就ok了。

3.支持版本回退(只需要将上一个版本编号改为最新版本号,并生成更新资源包)。

4.客户端代码减少,并且安全。

--------------------------------------------------------------------------------

今天我带来的是我将之前在cocoachina发表的版本完善后东西,包括服务端与客户端实现:

服务端包含了assertmanager需要的两个链接(版本与包地址的url,而且包地址必须还有.zip这样的字符串,不然会被判断无效,郁闷!)

也有预生成资源包的接口,可以在新版本发布的时候调用,用来生成最新的版本差异包。

客户端代码我是用了cocos code ide,不过也没怎么用它framework里面的东西,修改起来很简单,其实我提供的客户端代码更多算个代码示例(demo)。

废话不多说了。服务端使用可以参考server.zip中的readme.txt。

对了:欢迎大家对我这个新手批评与指正!

------------------------------------下载地址-----------------------------------

server:http://pan.baidu.com/s/1dD8Faxj

client:http://pan.baidu.com/s/1qW16ZZA

--------------------------------------------------------------------------------

注:由于code ide中对lua自带的文件系统lfs不支持,因此我使用了cocos2d-x自带luatest中的文件创建的接口,并将其导出至项目中。因此如果要使用客户端项目时需要注意将createDownloadDir与deleteDownloadDir两个函数导出,c++代码位置为:%engine_root%\tests\lua-tests\project\Classes\lua_assetsmanager_test_sample.cpp(.h),当然你可以选择不创建文件夹,而将更新文件直接解压到可写路径之下!貌似assertmanager可以自己创建存储路径,不过我还未测试。

另外,强烈希望cocos2d-lua能支持lfs或者写出自己功能更强的文件系统!



================================================================================================================================================================




转载于http://blog.csdn.net/linchaolong/article/details/42321767



在cocos2dx 3.4 运行通过


下载地址为:在我的下载里面


【Cocos2d-x】实现资源热更新

分类:Cocos2d-x 461人阅读 评论(0) 收藏 举报

目录(?)[+]


热更新介绍


什么是热更新?

游戏客户端启动时,主动请求服务端检查版本号,并更新资源到本地。


应用场景:

情况一:游戏客户端已经发布了,但突然发现有个比较严重的bug需要修复。这时需要更新游戏的代码(Lua代码)。

情况二:情人节到了,需要搞个活动,在游戏中营造一个节日氛围。这时,需要更新游戏资源或增加一些功能。


好处:不需要重新打包和提交应用到市场等待审核。



热更新流程





AssetsManager


在Cocos2d-x中已经封装了用于实现热更新功能的类,就是AssetsManager。


api说明:

// 检测是否有版本更新
virtual bool checkUpdate();

// 下载更新的资源包并解压到下载路径
virtual void update();

// 获取当前客户端版本号
std::string getVersion();

// 删除客户端版本号
void deleteVersion();

// 设置下载回调(AssetsManagerDelegateProtocol)
void setDelegate(AssetsManagerDelegateProtocol *delegate);

// 设置连接超时时间(单位:秒)
void setConnectionTimeout(unsigned int timeout);

// 设置从服务端下载资源包的url
void setPackageUrl(const char* packageUrl);

// 设置服务端获取版本号的url
void setVersionFileUrl(const char* versionFileUrl);

// 设置资源保存路径
void setStoragePath(const char* storagePath);


因为AssetsManager使用了pthread库,所以需要在win32工程中需要包含pthread库所在目录。

VS在工程属性——C/C++——常规——附加包含目录中添加:$(ProjectDir)..\..\..\cocos2dx\platform\third_party\win32\pthread



示例工程


该工程使用Cocos2d-x2.1.6和VS2012。【点击下载源码】


把工程放到引擎projects目录下即可。


<a target=_blank id="L1" href="http://blog.csdn.net/linchaolong/article/details/42321767#L1" rel="#L1" style="text-decoration: none; color: rgb(12,207);">   1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/linchaolong/article/details/42321767#L2" rel="#L2" style="text-decoration: none; color: rgb(12,207);">   2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/linchaolong/article/details/42321767#L3" rel="#L3" style="text-decoration: none; color: rgb(12,207);">   3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/linchaolong/article/details/42321767#L4" rel="#L4" style="text-decoration: none; color: rgb(12,207);">   4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/linchaolong/article/details/42321767#L5" rel="#L5" style="text-decoration: none; color: rgb(12,207);">   5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/linchaolong/article/details/42321767#L6" rel="#L6" style="text-decoration: none; color: rgb(12,207);">   6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/linchaolong/article/details/42321767#L7" rel="#L7" style="text-decoration: none; color: rgb(12,207);">   7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/linchaolong/article/details/42321767#L8" rel="#L8" style="text-decoration: none; color: rgb(12,207);">   8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/linchaolong/article/details/42321767#L9" rel="#L9" style="text-decoration: none; color: rgb(12,207);">   9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/linchaolong/article/details/42321767#L10" rel="#L10" style="text-decoration: none; color: rgb(12,207);">  10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/linchaolong/article/details/42321767#L11" rel="#L11" style="text-decoration: none; color: rgb(12,207);">  11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/linchaolong/article/details/42321767#L12" rel="#L12" style="text-decoration: none; color: rgb(12,207);">  12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/linchaolong/article/details/42321767#L13" rel="#L13" style="text-decoration: none; color: rgb(12,207);">  13</a>
<a target=_blank id="L14" href="http://blog.csdn.net/linchaolong/article/details/42321767#L14" rel="#L14" style="text-decoration: none; color: rgb(12,207);">  14</a>
<a target=_blank id="L15" href="http://blog.csdn.net/linchaolong/article/details/42321767#L15" rel="#L15" style="text-decoration: none; color: rgb(12,207);">  15</a>
<a target=_blank id="L16" href="http://blog.csdn.net/linchaolong/article/details/42321767#L16" rel="#L16" style="text-decoration: none; color: rgb(12,207);">  16</a>
<a target=_blank id="L17" href="http://blog.csdn.net/linchaolong/article/details/42321767#L17" rel="#L17" style="text-decoration: none; color: rgb(12,207);">  17</a>
<a target=_blank id="L18" href="http://blog.csdn.net/linchaolong/article/details/42321767#L18" rel="#L18" style="text-decoration: none; color: rgb(12,207);">  18</a>
<a target=_blank id="L19" href="http://blog.csdn.net/linchaolong/article/details/42321767#L19" rel="#L19" style="text-decoration: none; color: rgb(12,207);">  19</a>
<a target=_blank id="L20" href="http://blog.csdn.net/linchaolong/article/details/42321767#L20" rel="#L20" style="text-decoration: none; color: rgb(12,207);">  20</a>
<a target=_blank id="L21" href="http://blog.csdn.net/linchaolong/article/details/42321767#L21" rel="#L21" style="text-decoration: none; color: rgb(12,207);">  21</a>
<a target=_blank id="L22" href="http://blog.csdn.net/linchaolong/article/details/42321767#L22" rel="#L22" style="text-decoration: none; color: rgb(12,207);">  22</a>
<a target=_blank id="L23" href="http://blog.csdn.net/linchaolong/article/details/42321767#L23" rel="#L23" style="text-decoration: none; color: rgb(12,207);">  23</a>
<a target=_blank id="L24" href="http://blog.csdn.net/linchaolong/article/details/42321767#L24" rel="#L24" style="text-decoration: none; color: rgb(12,207);">  24</a>
<a target=_blank id="L25" href="http://blog.csdn.net/linchaolong/article/details/42321767#L25" rel="#L25" style="text-decoration: none; color: rgb(12,207);">  25</a>
<a target=_blank id="L26" href="http://blog.csdn.net/linchaolong/article/details/42321767#L26" rel="#L26" style="text-decoration: none; color: rgb(12,207);">  26</a>
<a target=_blank id="L27" href="http://blog.csdn.net/linchaolong/article/details/42321767#L27" rel="#L27" style="text-decoration: none; color: rgb(12,207);">  27</a>
<a target=_blank id="L28" href="http://blog.csdn.net/linchaolong/article/details/42321767#L28" rel="#L28" style="text-decoration: none; color: rgb(12,207);">  28</a>
<a target=_blank id="L29" href="http://blog.csdn.net/linchaolong/article/details/42321767#L29" rel="#L29" style="text-decoration: none; color: rgb(12,207);">  29</a>
<a target=_blank id="L30" href="http://blog.csdn.net/linchaolong/article/details/42321767#L30" rel="#L30" style="text-decoration: none; color: rgb(12,207);">  30</a>
<a target=_blank id="L31" href="http://blog.csdn.net/linchaolong/article/details/42321767#L31" rel="#L31" style="text-decoration: none; color: rgb(12,207);">  31</a>
<a target=_blank id="L32" href="http://blog.csdn.net/linchaolong/article/details/42321767#L32" rel="#L32" style="text-decoration: none; color: rgb(12,207);">  32</a>
<a target=_blank id="L33" href="http://blog.csdn.net/linchaolong/article/details/42321767#L33" rel="#L33" style="text-decoration: none; color: rgb(12,207);">  33</a>
<a target=_blank id="L34" href="http://blog.csdn.net/linchaolong/article/details/42321767#L34" rel="#L34" style="text-decoration: none; color: rgb(12,207);">  34</a>
<a target=_blank id="L35" href="http://blog.csdn.net/linchaolong/article/details/42321767#L35" rel="#L35" style="text-decoration: none; color: rgb(12,207);">  35</a>
<a target=_blank id="L36" href="http://blog.csdn.net/linchaolong/article/details/42321767#L36" rel="#L36" style="text-decoration: none; color: rgb(12,207);">  36</a>
<a target=_blank id="L37" href="http://blog.csdn.net/linchaolong/article/details/42321767#L37" rel="#L37" style="text-decoration: none; color: rgb(12,207);">  37</a>
<a target=_blank id="L38" href="http://blog.csdn.net/linchaolong/article/details/42321767#L38" rel="#L38" style="text-decoration: none; color: rgb(12,207);">  38</a>
<a target=_blank id="L39" href="http://blog.csdn.net/linchaolong/article/details/42321767#L39" rel="#L39" style="text-decoration: none; color: rgb(12,207);">  39</a>
<a target=_blank id="L40" href="http://blog.csdn.net/linchaolong/article/details/42321767#L40" rel="#L40" style="text-decoration: none; color: rgb(12,207);">  40</a>
<a target=_blank id="L41" href="http://blog.csdn.net/linchaolong/article/details/42321767#L41" rel="#L41" style="text-decoration: none; color: rgb(12,207);">  41</a>
<a target=_blank id="L42" href="http://blog.csdn.net/linchaolong/article/details/42321767#L42" rel="#L42" style="text-decoration: none; color: rgb(12,207);">  42</a>
<a target=_blank id="L43" href="http://blog.csdn.net/linchaolong/article/details/42321767#L43" rel="#L43" style="text-decoration: none; color: rgb(12,207);">  43</a>
<a target=_blank id="L44" href="http://blog.csdn.net/linchaolong/article/details/42321767#L44" rel="#L44" style="text-decoration: none; color: rgb(12,207);">  44</a>
<a target=_blank id="L45" href="http://blog.csdn.net/linchaolong/article/details/42321767#L45" rel="#L45" style="text-decoration: none; color: rgb(12,207);">  45</a>
<a target=_blank id="L46" href="http://blog.csdn.net/linchaolong/article/details/42321767#L46" rel="#L46" style="text-decoration: none; color: rgb(12,207);">  46</a>
<a target=_blank id="L47" href="http://blog.csdn.net/linchaolong/article/details/42321767#L47" rel="#L47" style="text-decoration: none; color: rgb(12,207);">  47</a>
<a target=_blank id="L48" href="http://blog.csdn.net/linchaolong/article/details/42321767#L48" rel="#L48" style="text-decoration: none; color: rgb(12,207);">  48</a>
<a target=_blank id="L49" href="http://blog.csdn.net/linchaolong/article/details/42321767#L49" rel="#L49" style="text-decoration: none; color: rgb(12,207);">  49</a>
<a target=_blank id="L50" href="http://blog.csdn.net/linchaolong/article/details/42321767#L50" rel="#L50" style="text-decoration: none; color: rgb(12,207);">  50</a>
<a target=_blank id="L51" href="http://blog.csdn.net/linchaolong/article/details/42321767#L51" rel="#L51" style="text-decoration: none; color: rgb(12,207);">  51</a>
<a target=_blank id="L52" href="http://blog.csdn.net/linchaolong/article/details/42321767#L52" rel="#L52" style="text-decoration: none; color: rgb(12,207);">  52</a>
<a target=_blank id="L53" href="http://blog.csdn.net/linchaolong/article/details/42321767#L53" rel="#L53" style="text-decoration: none; color: rgb(12,207);">  53</a>
<a target=_blank id="L54" href="http://blog.csdn.net/linchaolong/article/details/42321767#L54" rel="#L54" style="text-decoration: none; color: rgb(12,207);">  54</a>
<a target=_blank id="L55" href="http://blog.csdn.net/linchaolong/article/details/42321767#L55" rel="#L55" style="text-decoration: none; color: rgb(12,207);">  55</a>
<a target=_blank id="L56" href="http://blog.csdn.net/linchaolong/article/details/42321767#L56" rel="#L56" style="text-decoration: none; color: rgb(12,207);">  56</a>
<a target=_blank id="L57" href="http://blog.csdn.net/linchaolong/article/details/42321767#L57" rel="#L57" style="text-decoration: none; color: rgb(12,207);">  57</a>
<a target=_blank id="L58" href="http://blog.csdn.net/linchaolong/article/details/42321767#L58" rel="#L58" style="text-decoration: none; color: rgb(12,207);">  58</a>
<a target=_blank id="L59" href="http://blog.csdn.net/linchaolong/article/details/42321767#L59" rel="#L59" style="text-decoration: none; color: rgb(12,207);">  59</a>
<a target=_blank id="L60" href="http://blog.csdn.net/linchaolong/article/details/42321767#L60" rel="#L60" style="text-decoration: none; color: rgb(12,207);">  60</a>
<a target=_blank id="L61" href="http://blog.csdn.net/linchaolong/article/details/42321767#L61" rel="#L61" style="text-decoration: none; color: rgb(12,207);">  61</a>
<a target=_blank id="L62" href="http://blog.csdn.net/linchaolong/article/details/42321767#L62" rel="#L62" style="text-decoration: none; color: rgb(12,207);">  62</a>
<a target=_blank id="L63" href="http://blog.csdn.net/linchaolong/article/details/42321767#L63" rel="#L63" style="text-decoration: none; color: rgb(12,207);">  63</a>
<a target=_blank id="L64" href="http://blog.csdn.net/linchaolong/article/details/42321767#L64" rel="#L64" style="text-decoration: none; color: rgb(12,207);">  64</a>
<a target=_blank id="L65" href="http://blog.csdn.net/linchaolong/article/details/42321767#L65" rel="#L65" style="text-decoration: none; color: rgb(12,207);">  65</a>
<a target=_blank id="L66" href="http://blog.csdn.net/linchaolong/article/details/42321767#L66" rel="#L66" style="text-decoration: none; color: rgb(12,207);">  66</a>
<a target=_blank id="L67" href="http://blog.csdn.net/linchaolong/article/details/42321767#L67" rel="#L67" style="text-decoration: none; color: rgb(12,207);">  67</a>
<a target=_blank id="L68" href="http://blog.csdn.net/linchaolong/article/details/42321767#L68" rel="#L68" style="text-decoration: none; color: rgb(12,207);">  68</a>
<a target=_blank id="L69" href="http://blog.csdn.net/linchaolong/article/details/42321767#L69" rel="#L69" style="text-decoration: none; color: rgb(12,207);">  69</a>
<a target=_blank id="L70" href="http://blog.csdn.net/linchaolong/article/details/42321767#L70" rel="#L70" style="text-decoration: none; color: rgb(12,207);">  70</a>
<a target=_blank id="L71" href="http://blog.csdn.net/linchaolong/article/details/42321767#L71" rel="#L71" style="text-decoration: none; color: rgb(12,207);">  71</a>
<a target=_blank id="L72" href="http://blog.csdn.net/linchaolong/article/details/42321767#L72" rel="#L72" style="text-decoration: none; color: rgb(12,207);">  72</a>
<a target=_blank id="L73" href="http://blog.csdn.net/linchaolong/article/details/42321767#L73" rel="#L73" style="text-decoration: none; color: rgb(12,207);">  73</a>
<a target=_blank id="L74" href="http://blog.csdn.net/linchaolong/article/details/42321767#L74" rel="#L74" style="text-decoration: none; color: rgb(12,207);">  74</a>
<a target=_blank id="L75" href="http://blog.csdn.net/linchaolong/article/details/42321767#L75" rel="#L75" style="text-decoration: none; color: rgb(12,207);">  75</a>
<a target=_blank id="L76" href="http://blog.csdn.net/linchaolong/article/details/42321767#L76" rel="#L76" style="text-decoration: none; color: rgb(12,207);">  76</a>
<a target=_blank id="L77" href="http://blog.csdn.net/linchaolong/article/details/42321767#L77" rel="#L77" style="text-decoration: none; color: rgb(12,207);">  77</a>
<a target=_blank id="L78" href="http://blog.csdn.net/linchaolong/article/details/42321767#L78" rel="#L78" style="text-decoration: none; color: rgb(12,207);">  78</a>
<a target=_blank id="L79" href="http://blog.csdn.net/linchaolong/article/details/42321767#L79" rel="#L79" style="text-decoration: none; color: rgb(12,207);">  79</a>
<a target=_blank id="L80" href="http://blog.csdn.net/linchaolong/article/details/42321767#L80" rel="#L80" style="text-decoration: none; color: rgb(12,207);">  80</a>
<a target=_blank id="L81" href="http://blog.csdn.net/linchaolong/article/details/42321767#L81" rel="#L81" style="text-decoration: none; color: rgb(12,207);">  81</a>
<a target=_blank id="L82" href="http://blog.csdn.net/linchaolong/article/details/42321767#L82" rel="#L82" style="text-decoration: none; color: rgb(12,207);">  82</a>
<a target=_blank id="L83" href="http://blog.csdn.net/linchaolong/article/details/42321767#L83" rel="#L83" style="text-decoration: none; color: rgb(12,207);">  83</a>
<a target=_blank id="L84" href="http://blog.csdn.net/linchaolong/article/details/42321767#L84" rel="#L84" style="text-decoration: none; color: rgb(12,207);">  84</a>
<a target=_blank id="L85" href="http://blog.csdn.net/linchaolong/article/details/42321767#L85" rel="#L85" style="text-decoration: none; color: rgb(12,207);">  85</a>
<a target=_blank id="L86" href="http://blog.csdn.net/linchaolong/article/details/42321767#L86" rel="#L86" style="text-decoration: none; color: rgb(12,207);">  86</a>
<a target=_blank id="L87" href="http://blog.csdn.net/linchaolong/article/details/42321767#L87" rel="#L87" style="text-decoration: none; color: rgb(12,207);">  87</a>
<a target=_blank id="L88" href="http://blog.csdn.net/linchaolong/article/details/42321767#L88" rel="#L88" style="text-decoration: none; color: rgb(12,207);">  88</a>
<a target=_blank id="L89" href="http://blog.csdn.net/linchaolong/article/details/42321767#L89" rel="#L89" style="text-decoration: none; color: rgb(12,207);">  89</a>
<a target=_blank id="L90" href="http://blog.csdn.net/linchaolong/article/details/42321767#L90" rel="#L90" style="text-decoration: none; color: rgb(12,207);">  90</a>
<a target=_blank id="L91" href="http://blog.csdn.net/linchaolong/article/details/42321767#L91" rel="#L91" style="text-decoration: none; color: rgb(12,207);">  91</a>
<a target=_blank id="L92" href="http://blog.csdn.net/linchaolong/article/details/42321767#L92" rel="#L92" style="text-decoration: none; color: rgb(12,207);">  92</a>
<a target=_blank id="L93" href="http://blog.csdn.net/linchaolong/article/details/42321767#L93" rel="#L93" style="text-decoration: none; color: rgb(12,207);">  93</a>
<a target=_blank id="L94" href="http://blog.csdn.net/linchaolong/article/details/42321767#L94" rel="#L94" style="text-decoration: none; color: rgb(12,207);">  94</a>
<a target=_blank id="L95" href="http://blog.csdn.net/linchaolong/article/details/42321767#L95" rel="#L95" style="text-decoration: none; color: rgb(12,207);">  95</a>
<a target=_blank id="L96" href="http://blog.csdn.net/linchaolong/article/details/42321767#L96" rel="#L96" style="text-decoration: none; color: rgb(12,207);">  96</a>
<a target=_blank id="L97" href="http://blog.csdn.net/linchaolong/article/details/42321767#L97" rel="#L97" style="text-decoration: none; color: rgb(12,207);">  97</a>
<a target=_blank id="L98" href="http://blog.csdn.net/linchaolong/article/details/42321767#L98" rel="#L98" style="text-decoration: none; color: rgb(12,207);">  98</a>
<a target=_blank id="L99" href="http://blog.csdn.net/linchaolong/article/details/42321767#L99" rel="#L99" style="text-decoration: none; color: rgb(12,207);">  99</a>
<a target=_blank id="L100" href="http://blog.csdn.net/linchaolong/article/details/42321767#L100" rel="#L100" style="text-decoration: none; color: rgb(12,207);"> 100</a>
<a target=_blank id="L101" href="http://blog.csdn.net/linchaolong/article/details/42321767#L101" rel="#L101" style="text-decoration: none; color: rgb(12,207);"> 101</a>
<a target=_blank id="L102" href="http://blog.csdn.net/linchaolong/article/details/42321767#L102" rel="#L102" style="text-decoration: none; color: rgb(12,207);"> 102</a>
<a target=_blank id="L103" href="http://blog.csdn.net/linchaolong/article/details/42321767#L103" rel="#L103" style="text-decoration: none; color: rgb(12,207);"> 103</a>
<a target=_blank id="L104" href="http://blog.csdn.net/linchaolong/article/details/42321767#L104" rel="#L104" style="text-decoration: none; color: rgb(12,207);"> 104</a>
<a target=_blank id="L105" href="http://blog.csdn.net/linchaolong/article/details/42321767#L105" rel="#L105" style="text-decoration: none; color: rgb(12,207);"> 105</a>
<a target=_blank id="L106" href="http://blog.csdn.net/linchaolong/article/details/42321767#L106" rel="#L106" style="text-decoration: none; color: rgb(12,207);"> 106</a>
<a target=_blank id="L107" href="http://blog.csdn.net/linchaolong/article/details/42321767#L107" rel="#L107" style="text-decoration: none; color: rgb(12,207);"> 107</a>
<a target=_blank id="L108" href="http://blog.csdn.net/linchaolong/article/details/42321767#L108" rel="#L108" style="text-decoration: none; color: rgb(12,207);"> 108</a>
<a target=_blank id="L109" href="http://blog.csdn.net/linchaolong/article/details/42321767#L109" rel="#L109" style="text-decoration: none; color: rgb(12,207);"> 109</a>
<a target=_blank id="L110" href="http://blog.csdn.net/linchaolong/article/details/42321767#L110" rel="#L110" style="text-decoration: none; color: rgb(12,207);"> 110</a>
<a target=_blank id="L111" href="http://blog.csdn.net/linchaolong/article/details/42321767#L111" rel="#L111" style="text-decoration: none; color: rgb(12,207);"> 111</a>
<a target=_blank id="L112" href="http://blog.csdn.net/linchaolong/article/details/42321767#L112" rel="#L112" style="text-decoration: none; color: rgb(12,207);"> 112</a>
<a target=_blank id="L113" href="http://blog.csdn.net/linchaolong/article/details/42321767#L113" rel="#L113" style="text-decoration: none; color: rgb(12,207);"> 113</a>
<a target=_blank id="L114" href="http://blog.csdn.net/linchaolong/article/details/42321767#L114" rel="#L114" style="text-decoration: none; color: rgb(12,207);"> 114</a>
<a target=_blank id="L115" href="http://blog.csdn.net/linchaolong/article/details/42321767#L115" rel="#L115" style="text-decoration: none; color: rgb(12,207);"> 115</a>
<a target=_blank id="L116" href="http://blog.csdn.net/linchaolong/article/details/42321767#L116" rel="#L116" style="text-decoration: none; color: rgb(12,207);"> 116</a>
<a target=_blank id="L117" href="http://blog.csdn.net/linchaolong/article/details/42321767#L117" rel="#L117" style="text-decoration: none; color: rgb(12,207);"> 117</a>
<a target=_blank id="L118" href="http://blog.csdn.net/linchaolong/article/details/42321767#L118" rel="#L118" style="text-decoration: none; color: rgb(12,207);"> 118</a>
<a target=_blank id="L119" href="http://blog.csdn.net/linchaolong/article/details/42321767#L119" rel="#L119" style="text-decoration: none; color: rgb(12,207);"> 119</a>
<a target=_blank id="L120" href="http://blog.csdn.net/linchaolong/article/details/42321767#L120" rel="#L120" style="text-decoration: none; color: rgb(12,207);"> 120</a>
<a target=_blank id="L121" href="http://blog.csdn.net/linchaolong/article/details/42321767#L121" rel="#L121" style="text-decoration: none; color: rgb(12,207);"> 121</a>
<a target=_blank id="L122" href="http://blog.csdn.net/linchaolong/article/details/42321767#L122" rel="#L122" style="text-decoration: none; color: rgb(12,207);"> 122</a>
<a target=_blank id="L123" href="http://blog.csdn.net/linchaolong/article/details/42321767#L123" rel="#L123" style="text-decoration: none; color: rgb(12,207);"> 123</a>
<a target=_blank id="L124" href="http://blog.csdn.net/linchaolong/article/details/42321767#L124" rel="#L124" style="text-decoration: none; color: rgb(12,207);"> 124</a>
<a target=_blank id="L125" href="http://blog.csdn.net/linchaolong/article/details/42321767#L125" rel="#L125" style="text-decoration: none; color: rgb(12,207);"> 125</a>
<a target=_blank id="L126" href="http://blog.csdn.net/linchaolong/article/details/42321767#L126" rel="#L126" style="text-decoration: none; color: rgb(12,207);"> 126</a>
<a target=_blank id="L127" href="http://blog.csdn.net/linchaolong/article/details/42321767#L127" rel="#L127" style="text-decoration: none; color: rgb(12,207);"> 127</a>
<a target=_blank id="L128" href="http://blog.csdn.net/linchaolong/article/details/42321767#L128" rel="#L128" style="text-decoration: none; color: rgb(12,207);"> 128</a>
<a target=_blank id="L129" href="http://blog.csdn.net/linchaolong/article/details/42321767#L129" rel="#L129" style="text-decoration: none; color: rgb(12,207);"> 129</a>
<a target=_blank id="L130" href="http://blog.csdn.net/linchaolong/article/details/42321767#L130" rel="#L130" style="text-decoration: none; color: rgb(12,207);"> 130</a>
<a target=_blank id="L131" href="http://blog.csdn.net/linchaolong/article/details/42321767#L131" rel="#L131" style="text-decoration: none; color: rgb(12,207);"> 131</a>
<a target=_blank id="L132" href="http://blog.csdn.net/linchaolong/article/details/42321767#L132" rel="#L132" style="text-decoration: none; color: rgb(12,207);"> 132</a>
<a target=_blank id="L133" href="http://blog.csdn.net/linchaolong/article/details/42321767#L133" rel="#L133" style="text-decoration: none; color: rgb(12,207);"> 133</a>
<a target=_blank id="L134" href="http://blog.csdn.net/linchaolong/article/details/42321767#L134" rel="#L134" style="text-decoration: none; color: rgb(12,207);"> 134</a>
<a target=_blank id="L135" href="http://blog.csdn.net/linchaolong/article/details/42321767#L135" rel="#L135" style="text-decoration: none; color: rgb(12,207);"> 135</a>
<a target=_blank id="L136" href="http://blog.csdn.net/linchaolong/article/details/42321767#L136" rel="#L136" style="text-decoration: none; color: rgb(12,207);"> 136</a>
<a target=_blank id="L137" href="http://blog.csdn.net/linchaolong/article/details/42321767#L137" rel="#L137" style="text-decoration: none; color: rgb(12,207);"> 137</a>
<a target=_blank id="L138" href="http://blog.csdn.net/linchaolong/article/details/42321767#L138" rel="#L138" style="text-decoration: none; color: rgb(12,207);"> 138</a>
<a target=_blank id="L139" href="http://blog.csdn.net/linchaolong/article/details/42321767#L139" rel="#L139" style="text-decoration: none; color: rgb(12,207);"> 139</a>
<a target=_blank id="L140" href="http://blog.csdn.net/linchaolong/article/details/42321767#L140" rel="#L140" style="text-decoration: none; color: rgb(12,207);"> 140</a>
<a target=_blank id="L141" href="http://blog.csdn.net/linchaolong/article/details/42321767#L141" rel="#L141" style="text-decoration: none; color: rgb(12,207);"> 141</a>
<a target=_blank id="L142" href="http://blog.csdn.net/linchaolong/article/details/42321767#L142" rel="#L142" style="text-decoration: none; color: rgb(12,207);"> 142</a>
<a target=_blank id="L143" href="http://blog.csdn.net/linchaolong/article/details/42321767#L143" rel="#L143" style="text-decoration: none; color: rgb(12,207);"> 143</a>
<a target=_blank id="L144" href="http://blog.csdn.net/linchaolong/article/details/42321767#L144" rel="#L144" style="text-decoration: none; color: rgb(12,207);"> 144</a>
<a target=_blank id="L145" href="http://blog.csdn.net/linchaolong/article/details/42321767#L145" rel="#L145" style="text-decoration: none; color: rgb(12,207);"> 145</a>
<a target=_blank id="L146" href="http://blog.csdn.net/linchaolong/article/details/42321767#L146" rel="#L146" style="text-decoration: none; color: rgb(12,207);"> 146</a>
<a target=_blank id="L147" href="http://blog.csdn.net/linchaolong/article/details/42321767#L147" rel="#L147" style="text-decoration: none; color: rgb(12,207);"> 147</a>
<a target=_blank id="L148" href="http://blog.csdn.net/linchaolong/article/details/42321767#L148" rel="#L148" style="text-decoration: none; color: rgb(12,207);"> 148</a>
<a target=_blank id="L149" href="http://blog.csdn.net/linchaolong/article/details/42321767#L149" rel="#L149" style="text-decoration: none; color: rgb(12,207);"> 149</a>
<a target=_blank id="L150" href="http://blog.csdn.net/linchaolong/article/details/42321767#L150" rel="#L150" style="text-decoration: none; color: rgb(12,207);"> 150</a>
<a target=_blank id="L151" href="http://blog.csdn.net/linchaolong/article/details/42321767#L151" rel="#L151" style="text-decoration: none; color: rgb(12,207);"> 151</a>
<a target=_blank id="L152" href="http://blog.csdn.net/linchaolong/article/details/42321767#L152" rel="#L152" style="text-decoration: none; color: rgb(12,207);"> 152</a>
<a target=_blank id="L153" href="http://blog.csdn.net/linchaolong/article/details/42321767#L153" rel="#L153" style="text-decoration: none; color: rgb(12,207);"> 153</a>
<a target=_blank id="L154" href="http://blog.csdn.net/linchaolong/article/details/42321767#L154" rel="#L154" style="text-decoration: none; color: rgb(12,207);"> 154</a>
<a target=_blank id="L155" href="http://blog.csdn.net/linchaolong/article/details/42321767#L155" rel="#L155" style="text-decoration: none; color: rgb(12,207);"> 155</a>
<a target=_blank id="L156" href="http://blog.csdn.net/linchaolong/article/details/42321767#L156" rel="#L156" style="text-decoration: none; color: rgb(12,207);"> 156</a>
<a target=_blank id="L157" href="http://blog.csdn.net/linchaolong/article/details/42321767#L157" rel="#L157" style="text-decoration: none; color: rgb(12,207);"> 157</a>
<a target=_blank id="L158" href="http://blog.csdn.net/linchaolong/article/details/42321767#L158" rel="#L158" style="text-decoration: none; color: rgb(12,207);"> 158</a>
<a target=_blank id="L159" href="http://blog.csdn.net/linchaolong/article/details/42321767#L159" rel="#L159" style="text-decoration: none; color: rgb(12,207);"> 159</a>
<a target=_blank id="L160" href="http://blog.csdn.net/linchaolong/article/details/42321767#L160" rel="#L160" style="text-decoration: none; color: rgb(12,207);"> 160</a>
<a target=_blank id="L161" href="http://blog.csdn.net/linchaolong/article/details/42321767#L161" rel="#L161" style="text-decoration: none; color: rgb(12,207);"> 161</a>
<a target=_blank id="L162" href="http://blog.csdn.net/linchaolong/article/details/42321767#L162" rel="#L162" style="text-decoration: none; color: rgb(12,207);"> 162</a>
<a target=_blank id="L163" href="http://blog.csdn.net/linchaolong/article/details/42321767#L163" rel="#L163" style="text-decoration: none; color: rgb(12,207);"> 163</a>
<a target=_blank id="L164" href="http://blog.csdn.net/linchaolong/article/details/42321767#L164" rel="#L164" style="text-decoration: none; color: rgb(12,207);"> 164</a>
<a target=_blank id="L165" href="http://blog.csdn.net/linchaolong/article/details/42321767#L165" rel="#L165" style="text-decoration: none; color: rgb(12,207);"> 165</a>
<a target=_blank id="L166" href="http://blog.csdn.net/linchaolong/article/details/42321767#L166" rel="#L166" style="text-decoration: none; color: rgb(12,207);"> 166</a>
<a target=_blank id="L167" href="http://blog.csdn.net/linchaolong/article/details/42321767#L167" rel="#L167" style="text-decoration: none; color: rgb(12,207);"> 167</a>
<a target=_blank id="L168" href="http://blog.csdn.net/linchaolong/article/details/42321767#L168" rel="#L168" style="text-decoration: none; color: rgb(12,207);"> 168</a>
<a target=_blank id="L169" href="http://blog.csdn.net/linchaolong/article/details/42321767#L169" rel="#L169" style="text-decoration: none; color: rgb(12,207);"> 169</a>
<a target=_blank id="L170" href="http://blog.csdn.net/linchaolong/article/details/42321767#L170" rel="#L170" style="text-decoration: none; color: rgb(12,207);"> 170</a>
<a target=_blank id="L171" href="http://blog.csdn.net/linchaolong/article/details/42321767#L171" rel="#L171" style="text-decoration: none; color: rgb(12,207);"> 171</a>
<a target=_blank id="L172" href="http://blog.csdn.net/linchaolong/article/details/42321767#L172" rel="#L172" style="text-decoration: none; color: rgb(12,207);"> 172</a>
<a target=_blank id="L173" href="http://blog.csdn.net/linchaolong/article/details/42321767#L173" rel="#L173" style="text-decoration: none; color: rgb(12,207);"> 173</a>
<a target=_blank id="L174" href="http://blog.csdn.net/linchaolong/article/details/42321767#L174" rel="#L174" style="text-decoration: none; color: rgb(12,207);"> 174</a>
<a target=_blank id="L175" href="http://blog.csdn.net/linchaolong/article/details/42321767#L175" rel="#L175" style="text-decoration: none; color: rgb(12,207);"> 175</a>
<a target=_blank id="L176" href="http://blog.csdn.net/linchaolong/article/details/42321767#L176" rel="#L176" style="text-decoration: none; color: rgb(12,207);"> 176</a>
<a target=_blank id="L177" href="http://blog.csdn.net/linchaolong/article/details/42321767#L177" rel="#L177" style="text-decoration: none; color: rgb(12,207);"> 177</a>
<a target=_blank id="L178" href="http://blog.csdn.net/linchaolong/article/details/42321767#L178" rel="#L178" style="text-decoration: none; color: rgb(12,207);"> 178</a>
<a target=_blank id="L179" href="http://blog.csdn.net/linchaolong/article/details/42321767#L179" rel="#L179" style="text-decoration: none; color: rgb(12,207);"> 179</a>
<a target=_blank id="L180" href="http://blog.csdn.net/linchaolong/article/details/42321767#L180" rel="#L180" style="text-decoration: none; color: rgb(12,207);"> 180</a>
<a target=_blank id="L181" href="http://blog.csdn.net/linchaolong/article/details/42321767#L181" rel="#L181" style="text-decoration: none; color: rgb(12,207);"> 181</a>
<a target=_blank id="L182" href="http://blog.csdn.net/linchaolong/article/details/42321767#L182" rel="#L182" style="text-decoration: none; color: rgb(12,207);"> 182</a>
<a target=_blank id="L183" href="http://blog.csdn.net/linchaolong/article/details/42321767#L183" rel="#L183" style="text-decoration: none; color: rgb(12,207);"> 183</a>
<a target=_blank id="L184" href="http://blog.csdn.net/linchaolong/article/details/42321767#L184" rel="#L184" style="text-decoration: none; color: rgb(12,207);"> 184</a>
<a target=_blank id="L185" href="http://blog.csdn.net/linchaolong/article/details/42321767#L185" rel="#L185" style="text-decoration: none; color: rgb(12,207);"> 185</a>
<a target=_blank id="L186" href="http://blog.csdn.net/linchaolong/article/details/42321767#L186" rel="#L186" style="text-decoration: none; color: rgb(12,207);"> 186</a>
            
            
#include "UpdateLayer.h"
#include "HelloWorldScene.h"
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
#include <dirent.h>
#include <sys/stat.h>
#endif
bool UpdateLayer :: init (){
if ( CCLayer :: init ())
{
// 设置资源包下载目录
m_downloadDir = CCFileUtils :: sharedFileUtils () -> getWritablePath ();
m_downloadDir += "download" ;
// 设置代理
getAssetsManager () -> setDelegate ( this );
// 添加资源包下载路径到搜索路径,优先搜索更新的资源
std :: vector < std :: string > searchPaths = CCFileUtils :: sharedFileUtils () -> getSearchPaths ();
searchPaths . insert ( searchPaths . begin (), m_downloadDir );
CCFileUtils :: sharedFileUtils () -> setSearchPaths ( searchPaths );
// 提示
m_label = CCLabelTTF :: create ( "" , "Arial" , 18 );
m_label -> setAnchorPoint ( ccp ( 1 , 0.5 ));
m_label -> setPosition ( ccp ( 465 , 20 ));
addChild ( m_label );
// 菜单
CCMenu * menu = CCMenu :: create ();
menu -> setPosition ( CCPointZero );
addChild ( menu );
CCSize visibleSize = CCDirector :: sharedDirector () -> getVisibleSize ();
// 重置
CCMenuItemFont * itemReset = CCMenuItemFont :: create ( "reset" , this , menu_selector ( UpdateLayer :: reset ));
itemReset -> setPosition ( ccp ( visibleSize . width / 2 ,153)">50 ));
menu -> addChild ( itemReset );
// 获取当前版本号
CCMenuItemFont * itemGetClientVersion = CCMenuItemFont :: create ( "getClientVersion" , menu_selector ( UpdateLayer :: getClientVersion ));
itemGetClientVersion -> setPosition ( ccp ( visibleSize . width / 100 ));
menu -> addChild ( itemGetClientVersion );
// 获取服务器最新版本
CCMenuItemFont * itemGetServerVersion = CCMenuItemFont :: create ( "checkUpdate" , menu_selector ( UpdateLayer :: checkUpdate ));
itemGetServerVersion -> setPosition ( ccp ( visibleSize . width / 150 ));
menu -> addChild ( itemGetServerVersion );
// 更新版本
CCMenuItemFont * itemUpdateVersion = CCMenuItemFont :: create ( "updateVersion" , menu_selector ( UpdateLayer :: update ));
itemUpdateVersion -> setPosition ( ccp ( visibleSize . width / 200 ));
menu -> addChild ( itemUpdateVersion );
// 进入场景
CCMenuItemFont * itemEnterScene = CCMenuItemFont :: create ( "enterScene" , menu_selector ( UpdateLayer :: enterScene ));
itemEnterScene -> setPosition ( ccp ( visibleSize . width / 250 ));
menu -> addChild ( itemEnterScene );
return true ;
}
false ;
}
AssetsManager * UpdateLayer :: getAssetsManager (){
static AssetsManager * s_assetsManager = NULL ;
if ( s_assetsManager == NULL )
{
s_assetsManager = new AssetsManager ( "https://coding.net/u/linchaolong/p/Cocos2d-x_HotUpdate/git/raw/master/test.zip" , //下载资源包的url
"https://coding.net/u/linchaolong/p/Cocos2d-x_HotUpdate/git/raw/master/version" , // 获取服务端版本号的url
m_downloadDir . c_str ()); // 资源保存路径
s_assetsManager -> setDelegate ( this );
s_assetsManager -> setConnectionTimeout ( 3 );
}
CCLOG ( "save path : %s" , s_assetsManager -> getStoragePath ());
return s_assetsManager ;
}
void UpdateLayer :: initDownloadDir (){
// 如果下载目录不存在,则创建下载目录
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
DIR * pDir = NULL ;
pDir = opendir ( m_downloadDir . c_str ());
if ( ! pDir )
{
mkdir ( m_downloadDir . c_str (), S_IRWXU | S_IRWXG | S_IRWXO );
}
#else
if (( GetFileAttributesA ( m_downloadDir . c_str ())) == INVALID_FILE_ATTRIBUTES )
{
CreateDirectoryA ( m_downloadDir . c_str (),153)">0 );
}
#endif
}
void UpdateLayer :: deleteDir ( std :: string dir ){
// Remove downloaded files
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
std :: string command = "rm -r " ;
// Path may include space.
command += " \" " + dir + " ;
system ( command . c_str ());
#else
std :: string command = "rd /s /q " ;
// Path may include space.
command += " ;
system ( command . c_str ());
#endif
}
void UpdateLayer :: onError ( cocos2d :: extension :: AssetsManager :: ErrorCode errorCode ){
switch ( errorCode )
{
case cocos2d: : extension :: AssetsManager :: kCreateFile :
CCLOG ( "error : create file failure" );
m_label -> setString ( "error : create file failure" );
break ;
case cocos2d: : extension :: AssetsManager :: kNetwork :
CCLOG ( "error : no network" );
m_label -> setString ( "error : no network" );
break ;
case cocos2d: : extension :: AssetsManager :: kNoNewVersion :
CCLOG ( "error : no new version" );
m_label -> setString ( "error : no new version" );
break ;
case cocos2d: : extension :: AssetsManager :: kUncompress :
CCLOG ( "error : uncompress file error" );
m_label -> setString ( "error : uncompress file error" );
break ;
default:
break ;
}
}
void UpdateLayer :: onProgress ( int percent ){
char progress [ 80 ];
memset ( progress , '\0' , sizeof ( progress ) );
snprintf ( progress ,0); font-weight:bold">sizeof ( progress ),240)">"hotupdate downloading %d%%" , percent );
CCLOG ( "percent=%d %s" , percent , progress );
m_label -> setString ( progress );
}
void UpdateLayer :: onSuccess (){
CCLOG ( "download success." );
m_label -> setString ( "download success." );
}
void UpdateLayer :: update ( CCObject * pSender ){
// 初始化下载目录
initDownloadDir ();
// 下载更新包
getAssetsManager () -> update ();
}
void UpdateLayer :: reset ( CCObject * pSender ){
if ( "" != m_downloadDir )
{
// 删除下载目录
deleteDir ( m_downloadDir );
}
// 删除版本号
getAssetsManager () -> deleteVersion ();
}
void UpdateLayer :: getClientVersion ( CCObject * pSender ){
CCString * msg = CCString :: createWithFormat ( "current client version : %s" , getAssetsManager () -> getVersion (). c_str ());
CCLOG ( "%s" , msg -> getCString ());
m_label -> setString ( msg -> getCString ());
}
void UpdateLayer :: checkUpdate ( CCObject * pSender ){
if ( getAssetsManager () -> checkUpdate ())
{
CCLOG ( "has new version" );
m_label -> setString ( "has new version" );
} else {
CCLOG ( "has not new version" );
m_label -> setString ( "has not new version" );
}
}
void UpdateLayer :: enterScene ( CCObject * pSender ){
CCDirector :: sharedDirector () -> replaceScene ( HelloWorld :: scene ());
}
来自CODE的代码片
UpdateLayer.cpp
<a target=_blank id="L1" href="http://blog.csdn.net/linchaolong/article/details/42321767#L1" rel="#L1" style="text-decoration: none; color: rgb(12,207);"> 1</a> <a target=_blank id="L2" href="http://blog.csdn.net/linchaolong/article/details/42321767#L2" rel="#L2" style="text-decoration: none; color: rgb(12,207);"> 2</a> <a target=_blank id="L3" href="http://blog.csdn.net/linchaolong/article/details/42321767#L3" rel="#L3" style="text-decoration: none; color: rgb(12,207);"> 3</a> <a target=_blank id="L4" href="http://blog.csdn.net/linchaolong/article/details/42321767#L4" rel="#L4" style="text-decoration: none; color: rgb(12,207);"> 4</a> <a target=_blank id="L5" href="http://blog.csdn.net/linchaolong/article/details/42321767#L5" rel="#L5" style="text-decoration: none; color: rgb(12,207);"> 5</a> <a target=_blank id="L6" href="http://blog.csdn.net/linchaolong/article/details/42321767#L6" rel="#L6" style="text-decoration: none; color: rgb(12,207);"> 6</a> <a target=_blank id="L7" href="http://blog.csdn.net/linchaolong/article/details/42321767#L7" rel="#L7" style="text-decoration: none; color: rgb(12,207);"> 7</a> <a target=_blank id="L8" href="http://blog.csdn.net/linchaolong/article/details/42321767#L8" rel="#L8" style="text-decoration: none; color: rgb(12,207);"> 8</a> <a target=_blank id="L9" href="http://blog.csdn.net/linchaolong/article/details/42321767#L9" rel="#L9" style="text-decoration: none; color: rgb(12,207);"> 9</a> <a target=_blank id="L10" href="http://blog.csdn.net/linchaolong/article/details/42321767#L10" rel="#L10" style="text-decoration: none; color: rgb(12,207);"> 10</a> <a target=_blank id="L11" href="http://blog.csdn.net/linchaolong/article/details/42321767#L11" rel="#L11" style="text-decoration: none; color: rgb(12,207);"> 11</a> <a target=_blank id="L12" href="http://blog.csdn.net/linchaolong/article/details/42321767#L12" rel="#L12" style="text-decoration: none; color: rgb(12,207);"> 12</a> <a target=_blank id="L13" href="http://blog.csdn.net/linchaolong/article/details/42321767#L13" rel="#L13" style="text-decoration: none; color: rgb(12,207);"> 13</a> <a target=_blank id="L14" href="http://blog.csdn.net/linchaolong/article/details/42321767#L14" rel="#L14" style="text-decoration: none; color: rgb(12,207);"> 14</a> <a target=_blank id="L15" href="http://blog.csdn.net/linchaolong/article/details/42321767#L15" rel="#L15" style="text-decoration: none; color: rgb(12,207);"> 15</a> <a target=_blank id="L16" href="http://blog.csdn.net/linchaolong/article/details/42321767#L16" rel="#L16" style="text-decoration: none; color: rgb(12,207);"> 16</a> <a target=_blank id="L17" href="http://blog.csdn.net/linchaolong/article/details/42321767#L17" rel="#L17" style="text-decoration: none; color: rgb(12,207);"> 17</a> <a target=_blank id="L18" href="http://blog.csdn.net/linchaolong/article/details/42321767#L18" rel="#L18" style="text-decoration: none; color: rgb(12,207);"> 18</a> <a target=_blank id="L19" href="http://blog.csdn.net/linchaolong/article/details/42321767#L19" rel="#L19" style="text-decoration: none; color: rgb(12,207);"> 19</a> <a target=_blank id="L20" href="http://blog.csdn.net/linchaolong/article/details/42321767#L20" rel="#L20" style="text-decoration: none; color: rgb(12,207);"> 20</a> <a target=_blank id="L21" href="http://blog.csdn.net/linchaolong/article/details/42321767#L21" rel="#L21" style="text-decoration: none; color: rgb(12,207);"> 21</a> <a target=_blank id="L22" href="http://blog.csdn.net/linchaolong/article/details/42321767#L22" rel="#L22" style="text-decoration: none; color: rgb(12,207);"> 22</a> <a target=_blank id="L23" href="http://blog.csdn.net/linchaolong/article/details/42321767#L23" rel="#L23" style="text-decoration: none; color: rgb(12,207);"> 23</a> <a target=_blank id="L24" href="http://blog.csdn.net/linchaolong/article/details/42321767#L24" rel="#L24" style="text-decoration: none; color: rgb(12,207);"> 24</a> <a target=_blank id="L25" href="http://blog.csdn.net/linchaolong/article/details/42321767#L25" rel="#L25" style="text-decoration: none; color: rgb(12,207);"> 25</a> <a target=_blank id="L26" href="http://blog.csdn.net/linchaolong/article/details/42321767#L26" rel="#L26" style="text-decoration: none; color: rgb(12,207);"> 26</a> <a target=_blank id="L27" href="http://blog.csdn.net/linchaolong/article/details/42321767#L27" rel="#L27" style="text-decoration: none; color: rgb(12,207);"> 27</a> <a target=_blank id="L28" href="http://blog.csdn.net/linchaolong/article/details/42321767#L28" rel="#L28" style="text-decoration: none; color: rgb(12,207);"> 28</a> <a target=_blank id="L29" href="http://blog.csdn.net/linchaolong/article/details/42321767#L29" rel="#L29" style="text-decoration: none; color: rgb(12,207);"> 29</a> <a target=_blank id="L30" href="http://blog.csdn.net/linchaolong/article/details/42321767#L30" rel="#L30" style="text-decoration: none; color: rgb(12,207);"> 30</a> <a target=_blank id="L31" href="http://blog.csdn.net/linchaolong/article/details/42321767#L31" rel="#L31" style="text-decoration: none; color: rgb(12,207);"> 31</a> <a target=_blank id="L32" href="http://blog.csdn.net/linchaolong/article/details/42321767#L32" rel="#L32" style="text-decoration: none; color: rgb(12,207);"> 32</a> <a target=_blank id="L33" href="http://blog.csdn.net/linchaolong/article/details/42321767#L33" rel="#L33" style="text-decoration: none; color: rgb(12,207);"> 33</a> <a target=_blank id="L34" href="http://blog.csdn.net/linchaolong/article/details/42321767#L34" rel="#L34" style="text-decoration: none; color: rgb(12,207);"> 34</a> <a target=_blank id="L35" href="http://blog.csdn.net/linchaolong/article/details/42321767#L35" rel="#L35" style="text-decoration: none; color: rgb(12,207);"> 35</a> <a target=_blank id="L36" href="http://blog.csdn.net/linchaolong/article/details/42321767#L36" rel="#L36" style="text-decoration: none; color: rgb(12,207);"> 36</a> <a target=_blank id="L37" href="http://blog.csdn.net/linchaolong/article/details/42321767#L37" rel="#L37" style="text-decoration: none; color: rgb(12,207);"> 37</a> <a target=_blank id="L38" href="http://blog.csdn.net/linchaolong/article/details/42321767#L38" rel="#L38" style="text-decoration: none; color: rgb(12,207);"> 38</a> <a target=_blank id="L39" href="http://blog.csdn.net/linchaolong/article/details/42321767#L39" rel="#L39" style="text-decoration: none; color: rgb(12,207);"> 39</a> <a target=_blank id="L40" href="http://blog.csdn.net/linchaolong/article/details/42321767#L40" rel="#L40" style="text-decoration: none; color: rgb(12,207);"> 40</a> <a target=_blank id="L41" href="http://blog.csdn.net/linchaolong/article/details/42321767#L41" rel="#L41" style="text-decoration: none; color: rgb(12,207);"> 41</a> <a target=_blank id="L42" href="http://blog.csdn.net/linchaolong/article/details/42321767#L42" rel="#L42" style="text-decoration: none; color: rgb(12,207);"> 42</a> <a target=_blank id="L43" href="http://blog.csdn.net/linchaolong/article/details/42321767#L43" rel="#L43" style="text-decoration: none; color: rgb(12,207);"> 43</a> <a target=_blank id="L44" href="http://blog.csdn.net/linchaolong/article/details/42321767#L44" rel="#L44" style="text-decoration: none; color: rgb(12,207);"> 44</a> <a target=_blank id="L45" href="http://blog.csdn.net/linchaolong/article/details/42321767#L45" rel="#L45" style="text-decoration: none; color: rgb(12,207);"> 45</a> <a target=_blank id="L46" href="http://blog.csdn.net/linchaolong/article/details/42321767#L46" rel="#L46" style="text-decoration: none; color: rgb(12,207);"> 46</a> <a target=_blank id="L47" href="http://blog.csdn.net/linchaolong/article/details/42321767#L47" rel="#L47" style="text-decoration: none; color: rgb(12,207);"> 47</a> <a target=_blank id="L48" href="http://blog.csdn.net/linchaolong/article/details/42321767#L48" rel="#L48" style="text-decoration: none; color: rgb(12,207);"> 48</a> <a target=_blank id="L49" href="http://blog.csdn.net/linchaolong/article/details/42321767#L49" rel="#L49" style="text-decoration: none; color: rgb(12,207);"> 49</a> <a target=_blank id="L50" href="http://blog.csdn.net/linchaolong/article/details/42321767#L50" rel="#L50" style="text-decoration: none; color: rgb(12,207);"> 50</a> <a target=_blank id="L51" href="http://blog.csdn.net/linchaolong/article/details/42321767#L51" rel="#L51" style="text-decoration: none; color: rgb(12,207);"> 51</a> <a target=_blank id="L52" href="http://blog.csdn.net/linchaolong/article/details/42321767#L52" rel="#L52" style="text-decoration: none; color: rgb(12,207);"> 52</a> <a target=_blank id="L53" href="http://blog.csdn.net/linchaolong/article/details/42321767#L53" rel="#L53" style="text-decoration: none; color: rgb(12,207);"> 53</a> <a target=_blank id="L54" href="http://blog.csdn.net/linchaolong/article/details/42321767#L54" rel="#L54" style="text-decoration: none; color: rgb(12,207);"> 54</a>
#ifndef __HOTUPDATER_H__
#define __HOTUPDATER_H__
#include "cocos2d.h"
USING_NS_CC ;
#include "cocos-ext.h"
USING_NS_CC_EXT ;
#include "AssetsManager/AssetsManager.h"
// 热更新实现示例
class UpdateLayer : public CCLayer ,0); font-weight:bold">public AssetsManagerDelegateProtocol
{
public:
static CCScene * scene (){
CCScene * scene = CCScene :: create ();
scene -> addChild ( UpdateLayer :: create ());
return scene ;
};
static UpdateLayer * create (){
UpdateLayer * pLayer = new UpdateLayer ;
if ( pLayer && pLayer -> init ())
{
pLayer -> autorelease ();
return pLayer ;
}
delete pLayer ;
NULL ;
};
// 初始化
bool init ();
// 下载回调函数
virtual void onError ( cocos2d :: extension :: AssetsManager :: ErrorCode errorCode );
onProgress ( int percent );
onSuccess ();
// 菜单回调函数
reset ( CCObject * pSender ); // 重置版本
getClientVersion ( CCObject * pSender ); // 获取当前客户端版本号
checkUpdate ( CCObject * pSender ); // 检查是否有版本更新
update ( CCObject * pSender ); // 更新版本
enterScene ( CCObject * pSender ); // 进入场景,如果未更新屏幕中间会显示叹号的图片,更新后会显示另一张图片
protected:
// 初始化下载目录
initDownloadDir ();
// 删除目录
deleteDir ( std :: string dir );
private:
CCLabelTTF * m_label ;
std :: string m_downloadDir ;
AssetsManager * getAssetsManager ();
};
#endif
来自CODE的代码片
UpdateLayer.h


项目地址:https://coding.net/u/linchaolong/p/Cocos2d-x_HotUpdate/git

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


    本文实践自 RayWenderlich、Ali Hafizji 的文章《How To Create Dynamic Textures with CCRenderTexture in Cocos2D 2.X》,文中使用Cocos2D,我在这里使用Cocos2D-x 2.1.4进行学习和移植。在这篇文章,将会学习到如何创建实时纹理、如何用Gimp创建无缝拼接纹
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@163.com微信公众号:HopToad 欢迎转载,转载标注出处:http://blog.csdn.netotbaron/article/details/424343991.  软件准备 下载地址:http://cn.cocos2d-x.org/download 2.  简介2.1         引用C
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从Cocos2D-x官网上下载,进入网页http://www.cocos2d-x.org/download,点击Cocos2d-x以下的Download  v3.0,保存到自定义的文件夹2:从python官网上下载。进入网页https://www.python.org/downloads/,我当前下载的是3.4.0(当前最新
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发引擎,易学易用,支持多种智能移动平台。官网地址:http://cocos2d-x.org/当前版本:2.0    有很多的学习资料,在这里我只做为自己的笔记记录下来,错误之处还请指出。在VisualStudio2008平台的编译:1.下载当前稳
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《最强大脑》娱乐节目。将2048改造成一款挑战玩家对数字记忆的小游戏。邮箱:appdevzw@163.com微信公众号:HopToadAPK下载地址:http://download.csdn.net/detailotbaron/8446223源码下载地址:http://download.csdn.net/
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试以QtCreatorIDE来进行CMake构建。Cocos2d-x3.X地址:https://github.com/cocos2d/cocos2d-x1.打开QtCreator,菜单栏→"打开文件或项目...",打开cocos2d-x目录下的CMakeLists.txt文件;2.弹出CMake向导,如下图所示:设置
 下载地址:链接:https://pan.baidu.com/s/1IkQsMU6NoERAAQLcCUMcXQ提取码:p1pb下载完成后,解压进入build目录使用vs2013打开工程设置平台工具集,打开设置界面设置: 点击开始编译等待编译结束编译成功在build文件下会出现一个新文件夹Debug.win32,里面就是编译
分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net前言上次用象棋演示了cocos2dx的基本用法,但是对cocos2dx并没有作深入的讨论,这次以超级马里奥的源代码为线索,我们一起来学习超级马里奥的实
1. 圆形音量button事实上作者的本意应该是叫做“电位计button”。可是我觉得它和我们的圆形音量button非常像,所以就这么叫它吧~先看效果:好了,不多解释,本篇到此为止。(旁白: 噗。就这样结束了?)啊才怪~我们来看看代码:[cpp] viewplaincopyprint?CCContro
原文链接:http://www.cnblogs.com/physwf/archive/2013/04/26/3043912.html为了进一步深入学习贯彻Cocos2d,我们将自己写一个场景类,但我们不会走的太远,凡是都要循序渐进,哪怕只前进一点点,那也至少是前进了,总比贪多嚼不烂一头雾水的好。在上一节中我们建
2019独角兽企业重金招聘Python工程师标准>>>cocos2d2.0之后加入了一种九宫格的实现,主要作用是用来拉伸图片,这样的好处在于保留图片四个角不变形的同时,对图片中间部分进行拉伸,来满足一些控件的自适应(PS: 比如包括按钮,对话框,最直观的形象就是ios里的短信气泡了),这就要求图
原文链接:http://www.cnblogs.com/linji/p/3599478.html1.环境和工具准备Win7VS2010/2012,至于2008v2版本之后似乎就不支持了。 2.安装pythonv.2.0版本之前是用vs模板创建工程的,到vs2.2之后就改用python创建了。到python官网下载版本2.7.5的,然后
环境:ubuntu14.04adt-bundle-linux-x86_64android-ndk-r9d-linux-x86_64cocos2d-x-3.0正式版apache-ant1.9.3python2.7(ubuntu自带)加入环境变量exportANDROID_SDK_ROOT=/home/yangming/adt-bundle-linux/sdkexportPATH=${PATH}:/$ANDROID_SDK_ROOTools/export
1开发背景游戏程序设计涉及了学科中的各个方面,鉴于目的在于学习与进步,本游戏《FlappyBird》采用了两个不同的开发方式来开发本款游戏,一类直接采用win32底层API来实现,另一类采用当前火热的cocos2d-x游戏引擎来开发本游戏。2需求分析2.1数据分析本项目要开发的是一款游
原文链接:http://www.cnblogs.com/linji/p/3599912.html//纯色色块控件(锚点默认左下角)CCLayerColor*ccc=CCLayerColor::create(ccc4(255,0,0,128),200,100);//渐变色块控件CCLayerGradient*ccc=CCLayerGradient::create(ccc4(255,0,0,
原文链接:http://www.cnblogs.com/linji/p/3599488.html//载入一张图片CCSprite*leftDoor=CCSprite::create("loading/door.png");leftDoor->setAnchorPoint(ccp(1,0.5));//设置锚点为右边中心点leftDoor->setPosition(ccp(240,160));/
为了答谢广大学员对智捷课堂以及关老师的支持,现购买51CTO学院关老师的Cocos2d-x课程之一可以送智捷课堂编写图书一本(专题可以送3本)。一、Cocos2d-x课程列表:1、Cocos2d-x入门与提高视频教程__Part22、Cocos2d-x数据持久化与网络通信__Part33、Cocos2d-x架构设计与性能优化内存优
Spawn让多个action同时执行。Spawn有多种不同的create方法,最终都调用了createWithTwoActions(FiniteTimeAction*action1,FiniteTimeAction*action2)方法。createWithTwoActions调用initWithTwoActions方法:对两个action变量初始化:_one=action1;_two=action2;如果两个a
需要环境:php,luajit.昨天在cygwin上安装php和luajit环境,这真特么是一个坑。建议不要用虚拟环境安装打包环境,否则可能会出现各种莫名问题。折腾了一下午,最终将环境转向linux。其中,luajit的安装脚本已经在quick-cocos2d-x-develop/bin/中,直接luajit_install.sh即可。我的lin
v3.0相对v2.2来说,最引人注意的。应该是对触摸层级的优化。和lambda回调函数的引入(嗯嗯,不枉我改了那么多类名。话说,每次cocos2dx大更新。总要改掉一堆类名函数名)。这些特性应该有不少人研究了,所以今天说点跟图片有关的东西。v3.0在载入图片方面也有了非常大改变,仅仅只是