加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 20:01:47 所属栏目:百科 来源:网络整理
导读:转载地址:http://www.jb51.cc/article/p-uquxodan-bea.html 热更新介绍 什么是热更新? 游戏客户端启动时,主动请求服务端检查版本号,并更新资源到本地。 应用场景: 情况一:游戏客户端已经发布了,但突然发现有个比较严重的bug需要修复。这时需要更新游戏

转载地址:http://www.52php.cn/article/p-uquxodan-bea.html



热更新介绍


什么是热更新?

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

应用场景:

情况一:游戏客户端已经发布了,但突然发现有个比较严重的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)......cocos2dxplatformthird_partywin32pthread

示例工程

该工程使用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>
           
           
#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" ;
// 添加资源包下载路径到搜索路径,优先搜索更新的资源
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 ( "http://is.muni.cz/do/rect/el/ikony/sady/ikonky-128x128-png.zip" ,
"https://coding.net/u/linchaolong/p/Test/git/raw/master/README.md" ,
m_downloadDir . c_str ());
s_assetsManager -> setDelegate ( this );
s_assetsManager -> setConnectionTimeout ( 3 );
}
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 [ 20 ];
snprintf ( progress ,153)">20 ,240)">"downloading %d%%" , percent );
CCLOG ( "%s" , 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 ( 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 , 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



热更新介绍


什么是热更新?

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

应用场景:

情况一:游戏客户端已经发布了,但突然发现有个比较严重的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)......cocos2dxplatformthird_partywin32pthread

示例工程

【点击下载源码】

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

            
            
#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" ;
// 添加资源包下载路径到搜索路径,优先搜索更新的资源
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 ( "http://is.muni.cz/do/rect/el/ikony/sady/ikonky-128x128-png.zip" ,
"https://coding.net/u/linchaolong/p/Test/git/raw/master/README.md" ,
m_downloadDir . c_str ());
s_assetsManager -> setDelegate ( this );
s_assetsManager -> setConnectionTimeout ( 3 );
}
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 [ 20 ];
snprintf ( progress ,153)">20 ,240)">"downloading %d%%" , percent );
CCLOG ( "%s" , 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 ( 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
#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 , 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

相关内容
推荐文章
站长推荐
热点阅读