local BaseLoading=class("BaseLoading",function()
return cc.Layer:create()
end)
BaseLoading.init=function(self)
self._size=cc.Director:getInstance():getWinSize()
self._textures={}
self._curIndex=1 --当前loaded的图片编号
self:initTexture()
self:loadingUI()
end
--需要加载的所有资源文件
BaseLoading.initTexture=function(self)
end
--异步加载图片
BaseLoading.loadTexture=function(self)
local frameCache=cc.SpriteFrameCache:getInstance()
--加载图片
local function loadPng(texture)
self._label:setString(string.format("Loading...%d%%",math.floor(100*self._curIndex/#self._textures)))
cclog(string.format("loaded self.textures[%d]:%s",self._curIndex,self._textures[self._curIndex].loc..".png"))
if self._curIndex==#self._textures then
self:finishLoading()
end
self._curIndex=self._curIndex+1
end
--加载Plist图片
local function loadPlist(texture)
frameCache:addSpriteFrames(self._textures[self._curIndex].loc..".plist")
self._label:setString(string.format("Loading...%d%%",math.floor(100*self._curIndex/#self._textures)))
cclog(string.format("loaded self.textures[%d]:%s",self._curIndex,self._textures[self._curIndex].loc..".png"))
if self._curIndex==#self._textures then
self:finishLoading()
end
self._curIndex=self._curIndex+1
end
local textureCache=cc.Director:getInstance():getTextureCache()
for i=1,#self._textures do
local rtype=self._textures[i].rtype
if rtype=="png" then
textureCache:addImageAsync(self._textures[i].loc..".png",loadPng)
elseif rtype=="plist" then
textureCache:addImageAsync(self._textures[i].loc..".png",loadPlist)
end
end
self:unscheduleUpdate()
end
--loading UI
BaseLoading.loadingUI=function(self)
local label=cc.Label:createWithTTF("Loading...0%", "res/fonts/Marker Felt.ttf", 32)
label:setPosition(self._size.width/2,self._size.height/4)
self:addChild(label)
self._label=label
end
BaseLoading.startLoading=function(self)
self:registerScriptHandler(function(tag)
if tag=="enter" then
--设置定时器
self:scheduleUpdateWithPriorityLua(function(dt)
self:loadTexture()
end,0)
elseif tag=="exit" then
self:unscheduleUpdate()
end
end)
end
--完成异步加载图片回调函数
BaseLoading.finishLoading=function(self)
end
return BaseLoading