Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了【Cocos2d-x】Lua 资源热更新大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

什么是热更新

所谓的热更新,指的是客户端的更新。

大致的流程是,客户端在启动后访问更新的URL接口,根据更新接口的反馈,下载更新资源,然后使用新的资源启动客户端,或者直接使用新资源不重启客户端。


热更新代码使用到的场景

  • 情人节快到了,你想要组织一个游戏内活动,错过时机肯定是你最不想要看到的结果。

  • 当你发现一个严重的bug。

  • 当你想要添加一些新的场景或者关卡来延长游戏的生命。

  • 以及非常多其他的情况...


在Cocos2d-x引擎中的如何实现热更新

LuaENGIne

LuaENGIne是一个脚本能够实时运行Lua脚本的对象,也就是因为有了LuaENGIne这个C++类对象,所以才有了实现热更新技术的基础

  • 获取LuaENGIne脚本引擎的对象。

@H_772_57@
1
2
3
4
@H_673_73@//获取LuaENGIne引擎脚本类的单例对象
LuaENGIne*ENGIne=LuaENGIne::geTinstance();
//设置该单例对象作为脚本引擎管理器对象的脚本引擎
ScriptENGIneManager::geTinstance()->setScriptENGIne(ENGInE);
  • 使用LuaENGIne执行Lua字符串脚本

2
//使用Lua脚本引擎执行Lua字符串脚本
ENGIne->executeString( "print(\"Hello蓝鸥\")" );
  • 使用LuaENGIne执行Lua文件脚本

4
5
6
7
8
9
10
11
12
13
14
15
16
//获取储存的文件路径
std::stringpath=FileUtils::geTinstance()->getWritablePath();
path+= "Hellolanou.lua" ;
//创建一个文件指针
//路径、模式
FILE *file= fopen (path.c_str(), "w" );
@H_450_201@if (filE){
fputs ( "print(\"蓝鸥!!!\")" ,filE);
fclose (filE);
}
@H_618_228@ else
CCLOG( "savefileerror." );
//使用Lua脚本引擎执行Lua文件脚本
ENGIne->executeScriptFile(path.c_str());

lua_State

lua_State,可以认为是“脚本上下文”,主要包括当前Lua脚本环境的运行状态信息,还会有GC相关的信息。

使用COcos2d-x引擎开发时需要使用Lua,那么就需要连接到libcocos2d和libluacocos2d两个静态库。

也就是要在lua_State对象中注册对应的功能模块类,如果不想要使用里边相应的模块时,就可以在luamoduleregister.h中注释掉对应的一行代码。

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int lua_module_register(lua_State*L)
{
//注册cocosdenshion模块
register_cocosdenshion_module(L);
@H_944_327@//注册network网络模块
register_network_module(L);
#ifCC_USE_CCBUILDER
@H_944_327@//注册cocosbuilder模块
register_cocosbuilder_module(L);
#endif
#ifCC_USE_CCstuDIO
@H_618_228@ @H_944_327@//注册coccostudio模块
register_cocostudio_module(L);
#endif
@H_944_327@//注册ui模块
register_ui_moudle(L);
@H_944_327@//注册extension模块
register_extension_module(L);
#ifCC_USE_SPINE
@H_944_327@//注册spine模块
register_spine_module(L);
#endif
#ifCC_USE_3D
@H_944_327@//注册3d模块
register_cocos3d_module(L);
#endif
@H_944_327@//注册音频audio模块
register_audioENGIne_module(L);
return 1;
}

使用COcos2d-x引擎时需要使用Quick框架时,同样需要在lua_State注册quick框架的对应模块。

18
static void quick_module_register(lua_State*L)
luaopen_lua_extensions_more(L);
lua_getglobal(L,monospace!important; font-size:1em!important; min-height:inherit!important; color:blue!important; BACkground:none!important">"_G" );
(lua_istable(L,-1)) //stack:...,_G,
{
register_all_quick_manual(L);
@H_944_327@//extra
luaopen_cocos2dx_extra_luabinding(L);
register_all_cocos2dx_extension_filter(L);
@H_618_228@ luaopen_HelperFunc_luabinding(L);
#if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
luaopen_cocos2dx_extra_ios_iap_luabinding(L);
#endif
}
lua_pop(L,1);

LuaStack

通常情况下每一个lua_State对象就对应一个LuaStack。

使用到的相关代码:

10
//使用LuaENGIne对象获取Lua的函数栈
LuaStack*stack=ENGIne->getLuaStack();
#ifANYSDK_DEFINE>0
lua_getglobal(stack->getLuaState(),monospace!important; font-size:1em!important; min-height:inherit!important; BACkground:none!important">);
tolua_anysdk_open(stack->getLuaState());
tolua_anysdk_manual_open(stack->getLuaState());
lua_pop(stack->getLuaState(),1);
#endif
//设置加密用的密钥
stack->setXXTEAKeyAndSign( "2dxLua" :1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas, strlen ),monospace!important; font-size:1em!important; min-height:inherit!important; color:blue!important; BACkground:none!important">"XXTEA" ));

Assetsmanager

资源管理器的诞生就是为了在游戏运行时能够完成资源热更新的技术而设计的。

这里的资源可以是图片,音频甚至是游戏的脚本本身。

使用资源管理器,你将可以上传新的资源到你的服务器,你的游戏会跟踪远程服务器上的修改,将新的资源下载到用户的设备上并在游戏中使用新的资源。

这样的话,一个全新的设计,全新的游戏体验甚至全新的游戏内容就可以立刻被推送到用户的受伤。

更加重要的是,我们并不需要针对各个渠道去重新打包@R_442_9616@程序并且经历痛苦的应用审核,这个过程中没有任何成本!


  • 创建Assetsmanager对象

@H_267_618@ 17
static Assetsmanager*assetManager=NULL;
(!assetManager){
@H_944_327@/*创建Assetsmanager对象
@H_944_327@*@param资源包的下载路径
@H_944_327@*@param资源包的当前版本
@H_944_327@*@param资源包下载后的存储路径
@H_944_327@*/
assetManager= new Assetsmanager(
"http://project.lanou3g.com/game/cocos/teacher/test/src.zip" :1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"http://project.lanou3g.com/game/cocos/teacher/test/version.php" :1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
@H_618_228@ _pathToSave.c_str());
@H_944_327@//设置Assetsmanager对象的回调对象
assetManager->setDelegate( this );
@H_944_327@//设置Assetsmanager对象的timeout时间
assetManager->setConnectionTimeout(3);
AssetsmanagerDelegateProtocol

AssetsmanagerDelegateProtocal是一个类接口,主要用来封装下载过程中的回调接口。

26
class AssetsmanagerDelegateProtocol
{
public :
virtual ~AssetsmanagerDelegateProtocol(){};
:
@H_944_327@/*@briefCallBACkfunctionforerror
@H_944_327@@paramerrorCodeTypeoferror
@H_944_327@*@jsNA
@H_944_327@*@luaNA
@H_944_327@*/
virtual onError(Assetsmanager::ErrorCodeerrorCodE){};
@H_618_228@ @H_944_327@/**@briefCallBACkfunctionforrecordingdownloadingpercent
@H_944_327@@parampercentHowmuchpercentdownloaded
@H_944_327@@warningThiscallBACkfunctionjustforrecordingdownloadingpercent.
@H_944_327@Assetsmanagerwilldosomeotherthingafterdownloading,youshould
@H_944_327@writecodeinonsuccess()afterdownloading.
@H_944_327@*@jsNA
@H_944_327@*@luaNA
@H_944_327@*/
onProgress( percent){};
@H_944_327@/**@briefCallBACkfunctionforsuccess
@H_944_327@*@jsNA
@H_944_327@*@luaNA
@H_944_327@*/
onsuccess(){};
};

那么接下来,我们使用Assetsmanager来创建一个自动更新的类update.

update.h

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
//update.h
//Hello
//
//createdby蓝鸥.
//
//
#ifndef__Hello__update__
#define__Hello__update__
@H_618_228@ #include<stdio.h>
#include"cocos2d.h"
#include"extensions/cocos-ext.h"
update: cocos2d::Layer, cocos2d::extension::AssetsmanagerDelegateProtocol
{
:
virtual bool init();
update(cocos2d::ref*pSender);
reset(cocos2d::ref*pSender);
@H_944_327@//继承的回调函数
onError(cocos2d::extension::Assetsmanager::ErrorCodeerrorCodE);
percent);
onsuccess();
CREATE_FUNC(updatE);
private :
cocos2d::extension::Assetsmanager*getAssetsmanager();
@H_944_327@//创建下载到的目录路径
@H_197_1018@ initDownloadDir();
:
std::string_pathToSave;
@H_944_327@//用来显示下载进度的Label标签
cocos2d::Label*_showDownloadInfo;
};
#endif/*defined(__Hello__update__)*/

update.cpp

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
@H_944_1151@ 86
87
@H_772_1155@ 88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
@H_580_1197@ 109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
@H_280_1247@ 134
135
136
137
138
139
140
141
142
143
144
@H_944_1269@ 145
146
147
148
149
@H_673_1279@ 150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//update.cpp
#include"update.h"
#if(CC_TARGET_PLATFORM!=CC_PLATFORM_WIN32)
@H_618_228@
#include<dirent.h>
#include<sys/stat.h>
#endif
USING_NS_Cc;
USING_NS_CC_EXT;
#defineDOWNLOAD_FILE"download"
#include"CCLuaENGIne.h"
_pathToSave( "" :1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
_showDownloadInfo(NULL)
{
}
{
@H_197_1018@ Assetsmanager*assetManager=getAssetsmanager();
CC_SAFE_deletE(assetManager);
}
update::init()
{
(!Layer::init()){
return false ;
}
SizewinSize=Director::geTinstance()->getWinSize();
initDownloadDir();
_showDownloadInfo=Label::createWithSystemFont( "Arial" :1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,20);
_showDownloadInfo->setPosition(Vec2(winSize.width/2,winSize.height/2-20));
->addChild(_showDownloadInfo);
autoitemLabel1=MenuItemLabel::create(
Label::createWithSystemFont( "Reset" "Arail" ));
autoitemLabel2=MenuItemLabel::create(
"update" ));
automenu=Menu::create(itemLabel1,itemLabel2,null);
->addChild(menu);
itemLabel1->setPosition(Vec2(winSize.width/2,winSize.height/2+20));
itemLabel2->setPosition(Vec2(winSize.width/2,winSize.height/2));
@H_269_175@menu->setPosition(Vec2::ZERO);
true ;
}
update::onError(Assetsmanager::ErrorCodecodE)
{
switch (codE){
case cocos2d::extension::Assetsmanager::ErrorCode::NO_NEW_VERSION:
_showDownloadInfo->setString( "nonewversion" );
break ;
@H_453_1673@ cocos2d::extension::Assetsmanager::ErrorCode::NETWORK:
);
;
cocos2d::extension::Assetsmanager::ErrorCode::CREATE_FILE:
"createfileerror" );
;
default :
@H_674_1705@ ;
}
}
@H_935_1717@
update::onProgress( percent)
{
(percent<0){
;
}
char progress[20];
snprintf(progress,monospace!important; font-size:1em!important; min-height:inherit!important; color:blue!important; BACkground:none!important">"download%d%%" :1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,percent);
_showDownloadInfo->setString(progress);
}
@H_343_1772@
update::onsuccess()
{
"downloadsuccess" );
);
std::stringpath=FileUtils::geTinstance()->getWritablePath()+DOWNLOAD_FILE;
LuaENGIne*pENGIne=LuaENGIne::geTinstance();
@H_944_327@//首先添加下载文件的目录
pENGIne->addSearchPath(_pathToSave.c_str());
"/src/main.lua" ;
pENGIne->executeScriptFile(path.c_str());
}
Assetsmanager*update::getAssetsmanager()
{
Assetsmanager*assetManager=NULL;
(!assetManager){
@H_944_327@/*创建Assetsmanager对象
@H_944_327@*@param资源包的下载路径
@H_673_1884@ @H_944_327@*@param资源包的当前版本
@H_944_327@*@param资源包下载后的存储路径
@H_944_327@*/
Assetsmanager(
:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
@H_105_1906@ _pathToSave.c_str());
@H_944_327@//设置Assetsmanager对象的回调对象
);
@H_944_327@//设置Assetsmanager对象的timeout时间
assetManager->setConnectionTimeout(3);
}
assetManager;
}
update::initDownloadDir()
{
"initDownloadDir" );
_pathToSave=FileUtils::geTinstance()->getWritablePath();
_pathToSave+=DOWNLOAD_FILE;
@H_669_1972@ @H_12_1976@ "Path:%s" :1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,_pathToSave.c_str());
#if(CC_TARGET_PLATFORM!=CC_PLATFORM_WIN32)
DIR*pDir=NULL;
pDir=opendir(_pathToSave.c_str());
(!pDir){
@H_269_175@mkdir(_pathToSave.c_str(),S_IRWXU|S_IRWXG|S_IRWXO);
}
#else
((GetFileAttributes(_pathToSave.c_str()))=INVALID_FILE_ATTRIBUTES){
CreateDirectoryA(_pathToSave.c_str(),0);
}
#endif
"initDownloadDirend" );
}
update::reset(Ref*pSender)
{
);
@H_944_327@//Removedownloadedfiles
#if(CC_TARGET_PLATFORM!=CC_PLATFORM_WIN32)
std::stringcommand= "rm-r" ;
@H_944_327@//Pathmayincludespace.
command+= "\"" +_pathToSave+ ;
system (command.c_str());
#else
"rd/s/q" ;
@H_944_327@//Pathmayincludespace.
;
(command.c_str());
#endif
getAssetsmanager()->deleteVersion();
initDownloadDir();
}
update::update(Ref*pSender)
{
);
getAssetsmanager()->update();
}

可以点击下载下来网络资源压缩包,看下压缩包内包含的内容。

可以点击下载下来源代码压缩包,对应一起的。

大家最好可以在本地搭建一个apache服务器,做一下练习。



转自:http://www.cnblogs.com/daxiaxiaohao/p/4162400.html

大佬总结

以上是大佬教程为你收集整理的【Cocos2d-x】Lua 资源热更新全部内容,希望文章能够帮你解决【Cocos2d-x】Lua 资源热更新所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。