1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
local AssetsManager = class("AssetsManager",function () return cc.LayerColor:create(cc.c4b(20, 20, 20, 220)) end) function AssetsManager:ctor() self:onNodeEvent("exit", handler(self, self.onExitCallback)) self:initUI() self:setAssetsManage() end function AssetsManager:onExitCallback() self.assetsManagerEx:release() end function AssetsManager:initUI() local hintLabel = cc.Label:createWithTTF("正在更新...", CONFIG.TTF_FONT_2, 20) :addTo(self) :move(600, 80) local progressBg = display.newSprite("sprites/hyd_progress_bg.png") :addTo(self) :move(600, 40) self.progress = cc.ProgressTimer:create(display.newSprite("sprites/hyd_progress.png")) :addTo(progressBg) :move(380, 19) self.progress:setType(cc.PROGRESS_TIMER_TYPE_BAR) self.progress:setBarChangeRate(cc.p(1, 0)) self.progress:setMidpoint(cc.p(0.0, 0.5)) self.progress:setPercentage(0) self.listener = cc.EventListenerTouchOneByOne:create() self.listener:setSwallowTouches(true) local onTouchBegan = function (touch, event) return true end self.listener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN) cc.Director:getInstance():getEventDispatcher():addEventListenerWithSceneGraphPriority(self.listener, self) end function AssetsManager:setAssetsManage() local storagePath = cc.FileUtils:getInstance():getWritablePath() .. "NewRes/" local resPath = storagePath.. '/res/' local srcPath = storagePath.. '/src/' if not (cc.FileUtils:getInstance():isDirectoryExist(storagePath)) then cc.FileUtils:getInstance():createDirectory(storagePath) cc.FileUtils:getInstance():createDirectory(resPath) cc.FileUtils:getInstance():createDirectory(srcPath) end local searchPaths = cc.FileUtils:getInstance():getSearchPaths() table.insert(searchPaths, 1, storagePath) table.insert(searchPaths, 2, resPath) table.insert(searchPaths, 3, srcPath) cc.FileUtils:getInstance():setSearchPaths(searchPaths) self.assetsManagerEx = cc.AssetsManagerEx:create("version/project.manifest", storagePath) self.assetsManagerEx:retain() local eventListenerAssetsManagerEx = cc.EventListenerAssetsManagerEx:create(self.assetsManagerEx, function (event) self:handleAssetsManagerEvent(event) end) local dispatcher = cc.Director:getInstance():getEventDispatcher() dispatcher:addEventListenerWithFixedPriority(eventListenerAssetsManagerEx, 1) self.assetsManagerEx:update() end function AssetsManager:handleAssetsManagerEvent(event) local eventCodeList = cc.EventAssetsManagerEx.EventCode local eventCodeHand = { [eventCodeList.ERROR_NO_LOCAL_MANIFEST] = function () print("发生错误:本地资源清单文件未找到") end, [eventCodeList.ERROR_DOWNLOAD_MANIFEST] = function () print("发生错误:远程资源清单文件下载失败") self:downloadManifestError() end, [eventCodeList.ERROR_PARSE_MANIFEST] = function () print("发生错误:资源清单文件解析失败") end, [eventCodeList.NEW_VERSION_FOUND] = function () print("发现找到新版本") end, [eventCodeList.ALREADY_UP_TO_DATE] = function () print("已经更新到服务器最新版本") self:updateFinished() end, [eventCodeList.UPDATE_PROGRESSION]= function () print("更新过程的进度事件") self.progress:setPercentage(event:getPercentByFile()) end, [eventCodeList.ASSET_UPDATED] = function () print("单个资源被更新事件") end, [eventCodeList.ERROR_UPDATING] = function () print("发生错误:更新过程中遇到错误") end, [eventCodeList.UPDATE_FINISHED] = function () print("更新成功事件") self:updateFinished() end, [eventCodeList.UPDATE_FAILED] = function () print("更新失败事件") end, [eventCodeList.ERROR_DECOMPRESS] = function () print("解压缩失败") end } local eventCode = event:getEventCode() if eventCodeHand[eventCode] ~= nil then eventCodeHand[eventCode]() end end function AssetsManager:updateFinished() self:setVisible(false) self.listener:setEnabled(false) end function AssetsManager:downloadManifestError() self:setVisible(false) self.listener:setEnabled(false) end return AssetsManager
|