在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开发环境:WIN7 + cocos2dx 3.10 lua版本 0.学习这篇内容的基础是你要会创建并运行一个cocos lua项目 1.热更新的思想 2.热更新用到的类: 3.最简单的示例: cc.FileUtils:getInstance():setPopupNotify(false) cc.FileUtils:getInstance():addSearchPath("src/") cc.FileUtils:getInstance():addSearchPath("res/") require "config" require "cocos.init" local function main() print("开始启动程序") --初始界面 local _Director = cc.Director:getInstance() local _Wide = display.width local _Hight = display.height local scene = cc.Scene:create() if _Director:getRunningScene() then _Director:replaceScene(scene) else _Director:runWithScene(scene) end local spHelloWorld = cc.Sprite:create("HelloWorld.png") spHelloWorld:setPosition(_Wide / 2, _Hight / 2) spHelloWorld:addTo(scene,9) local text = cc.Label:createWithSystemFont("Hello World", "Arial", 40) text:move(display.cx, display.cy + 200) text:addTo(scene,9) --更新完成后的界面 local function run() print("启动完成") local bg = cc.Sprite:create("Images/Tranquil_Lagoon.jpg") bg:setPosition(_Wide / 2, _Hight / 2) bg:addTo(scene) text:setString("Hello World 更新完成") end --热更新开始 local writablePath = cc.FileUtils:getInstance():getWritablePath() local storagePath = writablePath .. "new_version" --将下载目录的src和res作为优先级最高的搜索目录,这样才能保证下载的能覆盖原来的代码 cc.FileUtils:getInstance():addSearchPath(storagePath.."/src/",true) cc.FileUtils:getInstance():addSearchPath(storagePath.."/res/",true) print("下载路径:"..storagePath) -- 创建AssetsManagerEx对象 --参数:本地project.manifest 下载保存路径 local assetsManagerEx = cc.AssetsManagerEx:create("src/version/project.manifest", storagePath) assetsManagerEx:retain() -- 设置下载消息listener local function handleAssetsManagerEx(event) if (cc.EventAssetsManagerEx.EventCode.ALREADY_UP_TO_DATE == event:getEventCode()) then print("已经是最新版本了,进入游戏主界面") run() end if (cc.EventAssetsManagerEx.EventCode.NEW_VERSION_FOUND == event:getEventCode()) then print("发现新版本,开始升级") end if (cc.EventAssetsManagerEx.EventCode.UPDATE_PROGRESSION == event:getEventCode()) then print("更新进度=" .. event:getPercent()) end if (cc.EventAssetsManagerEx.EventCode.UPDATE_FINISHED == event:getEventCode()) then print("更新完毕,重新启动") run() end if (cc.EventAssetsManagerEx.EventCode.ERROR_NO_LOCAL_MANIFEST == event:getEventCode()) then print("发生错误:本地找不到manifest文件") end if (cc.EventAssetsManagerEx.EventCode.ERROR_DOWNLOAD_MANIFEST == event:getEventCode()) then print("发生错误:下载manifest文件失败") end if (cc.EventAssetsManagerEx.EventCode.ERROR_PARSE_MANIFEST == event:getEventCode()) then print("发生错误:解析manifest文件失败") end if (cc.EventAssetsManagerEx.EventCode.ERROR_UPDATING == event:getEventCode()) then print("发生错误:更新失败") end end --热更新事件 local dispatcher = cc.Director:getInstance():getEventDispatcher() local eventListenerAssetsManagerEx = cc.EventListenerAssetsManagerEx:create(assetsManagerEx, handleAssetsManagerEx) dispatcher:addEventListenerWithFixedPriority(eventListenerAssetsManagerEx, 1) local localManifest = assetsManagerEx:getLocalManifest() print("当前版本:"..localManifest:getVersion()) -- 检查版本并升级 assetsManagerEx:update() end local status, msg = xpcall(main, __G__TRACKBACK__) if not status then print(msg) end
|
请发表评论