--local MainSceneConfig = require "res.scripts.configs.MainSceneConfig" -- 暂时添加一个临时配置文件
--require "res.scripts.scenes.MainScene" -- 加载事件
OnLoadingStart = "OnLoadingStart";
OnLoadingProcess = "OnLoadingProcess";
OnLoadingEnd = "OnLoadingEnd"; -- 加载类型
SOUNDTYPE = "sounds";
TEXTURETYPE = "textures";
ATLASTYPE = "atlas";
ANIMATIONTYPE = "animations";
EFFECTTYPE = "effects"; -- 加载控制逻辑
local LoadingController = class("LoadingController", Behavior); function LoadingController:ctor(sceneName)
LoadingController.super.ctor(self); -- 定义成员变量
self.progress = ; -- 加载进度
self.totalLoadingNum = ; --加载总数数量
self.curLoadingNum = ; -- 当前加载数量
self.loadingConfig = ""; -- 资源配置文件 self.log = nil; -- 加载界面log
self.btmLabel = nil; --进度显示标签
self.uiLayout = nil; -- 加载界面布局
self.uiLayer = nil; -- 加载界面层
self.curScene = nil; -- 上一个场景
self.nextScene = nil; -- 下一个场景
self.config = res[sceneName];-- 配置文件
self.curSceneName = sceneName; self.isLoaded = false; -- 随机抽取一只怪物
local animKeys = table.keys(res.animations);
if #animKeys > then
local index = math.random(, #animKeys);
self.animName = tostring(animKeys[index]);
self.loadingxml = res.animations[self.animName];
self.loadingplist = string.gsub(self.loadingxml, ".xml", ".plist")
self.loadingpng = string.gsub (self.loadingxml, ".xml", ".png")
end
end function LoadingController:run( )
self.curScene = SceneM.getRunningScene();
self.nextScene = nil; -- 1.ui层
self.uiLayer = ObjectFactory.newLayer();
self.uiLayer:setColor(ccc3(, , )); -- 2.创建构成加载场景的元素
self.btmLabel = ObjectFactory.newBMFontLabel({text = "Loading 0%", font = res.fonts.UIFont,})
self.btmLabel:setPosition(ccp(Screen.cx, Screen.cy - ));
self.uiLayer:addChild(self.btmLabel);
if self.loadingxml then
CCArmatureDataManager:sharedArmatureDataManager():addArmatureFileInfoAsync(self.loadingpng, self.loadingplist, self.loadingxml, function ( )
if self.isLoaded then
return;
end
local lion = ObjectFactory.newArmature(self.animName);
lion:setPosition(ccp(Screen.cx,Screen.cy));
lion:addParent(self.uiLayer);
lion:getComponent("Animation"):play("run", WrapMode.Loop);
end)
end -- 3. 添加到场景
SceneM.dontDestroy(self.uiLayer); -- 4. 关掉触摸消息
CCDirector:sharedDirector():getTouchDispatcher():setDispatchEvents(false); -- 5. 关掉动作管理
CCDirector:sharedDirector():getActionManager():removeAllActions(); -- 6. 退出当前场景
if self.curScene then
self.curScene:setVisible(false);
end -- 7. 重新初始化管理类
SceneM.uiLayers = {}; -- 8.延迟清除当前资源,并且加载下一个场景资源
performWithDelay(function ( )
self:cleanup();
self:startLoading(self.config);
end, 0.01) -- 9. 回收lua内存
collectgarbage("collect")
end function LoadingController:startLoading(theloadingconfig)
if theloadingconfig == nil then
self.isLoaded = true;
-- 切换场景
self.nextScene = ObjectFactory.newScene(self.curSceneName);
SceneM.curScene = self.nextScene;
self.nextScene:addComponent(self.curSceneName,SceneM.getSceneController(self.curSceneName)); -- 关闭加载层
SceneM.destroy(self.uiLayer); -- 开启触摸消息
CCDirector:sharedDirector():getTouchDispatcher():setDispatchEvents(true); -- 记录配置文件路径
SceneM.lastSceneName = self.curSceneName; -- 切换到新场景
SceneM.replaceScene(self.nextScene, "crossfade", 0.2, Screen.COLOR_WHITE);
return;
end
print ("start loading")
self.loadingConfig = theloadingconfig;
local resTypes = {"sounds","textures","atlas","animations","effects"}
local curIndex = ;
-- 1.开始当前场景加载资源
self.curLoadingNum =
local totalNum =
local function loopRes( t , resType)
if self.curLoadingNum > self.totalLoadingNum then
return;
end
if t == nil then
curIndex = curIndex + ;
local resType = resTypes[curIndex];
print("加载类型:",resType);
print("加载:",theloadingconfig[resType]);
loopRes(theloadingconfig[resType], resType);
return;
end
local keys = table.keys(t);
totalNum = totalNum + #keys;
local tempIndex = ;
local function loadRes( )
self.curLoadingNum = self.curLoadingNum + ;
if self.curLoadingNum > self.totalLoadingNum then
return;
end
--print("====",self.curLoadingNum , totalNum)
if self.curLoadingNum > totalNum then
tempIndex = ;
curIndex = curIndex + ;
local resType = resTypes[curIndex];
self.curLoadingNum = self.curLoadingNum - ;
loopRes(theloadingconfig[resType], resType);
return;
end
local key = keys[tempIndex];
local value = t[key];
tempIndex = tempIndex + ;
if (tolua.type(value) == "table") then
loopRes( value );
elseif (tolua.type(value) == "string") then
if resType == SOUNDTYPE then
-- 1.异步声音加载
--print("加载声音:", value);
Audio.preloadEffect(value);
performWithDelay(function ( )
loadRes();
end, CCDirector:sharedDirector():deltaTime())
end if resType == TEXTURETYPE then
-- 2.异步纹理加载
--print("加载纹理:", value);
CCTextureCache:sharedTextureCache():addImageAsync(value, function ( )
self:CheckedResLoaded(); -- 检测下是否加载完毕
performWithDelay(function ( )
loadRes();
end, CCDirector:sharedDirector():deltaTime())
end);
end if resType == ATLASTYPE then
-- 2.异步纹理加载
local png = string.gsub(value, ".plist", ".png")
--print("加载图集:",png);
CCTextureCache:sharedTextureCache():addImageAsync(png, function ( )
-- 添加plist 到 CCSpriteFrameCache
CCSpriteFrameCache:sharedSpriteFrameCache():addSpriteFramesWithFile(value)
self:CheckedResLoaded(); -- 检测下是否加载完毕
performWithDelay(function ( )
loadRes();
end, CCDirector:sharedDirector():deltaTime())
end);
end if resType == ANIMATIONTYPE or resType == EFFECTTYPE then
--3.异步动画加载
local plist = string.gsub(value, ".xml", ".plist")
local png = string.gsub (value, ".xml", ".png")
--print("加载骨骼动画:",png, plist, value);
CCArmatureDataManager:sharedArmatureDataManager():addArmatureFileInfoAsync(png, plist, value, function ( )
self:CheckedResLoaded(); -- 检测下是否加载完毕
performWithDelay(function ( )
loadRes();
end, CCDirector:sharedDirector():deltaTime())
end)
end
end
end loadRes();
end
-- 计算加载的资源总数
local function size( config )
return #table.keys(config.textures)
+ #table.keys(config.sounds)
+ #table.keys(config.fonts)
+ #table.keys(config.animations)
+ #table.keys(config.atlas)
+ #table.keys(config.effects);
end
self.totalLoadingNum = size(theloadingconfig); -- 加载场景资源
local resType = resTypes[curIndex];
print("加载类型:",resType);
print("加载:",theloadingconfig[resType]);
loopRes(theloadingconfig[resType], resType);
end -- 检测是否加载完毕
function LoadingController:CheckedResLoaded( )
if self.curLoadingNum <= self.totalLoadingNum then
-- 1.更新当前进度
local percent = math.modf(self.curLoadingNum * / self.totalLoadingNum)
self.btmLabel:setString(string.format("Loading %s%%", percent));
end -- 2.检测下是否加载完毕
if (self.curLoadingNum == self.totalLoadingNum) then
local function onComplete(event)
print ("loading res finished!")
self.isLoaded = true;
-- 切换场景
self.nextScene = ObjectFactory.newScene(self.curSceneName);
SceneM.curScene = self.nextScene;
self.nextScene:addComponent(self.curSceneName,SceneM.getSceneController(self.curSceneName)); -- 关闭加载层
SceneM.destroy(self.uiLayer); -- 开启触摸消息
CCDirector:sharedDirector():getTouchDispatcher():setDispatchEvents(true); -- 记录配置文件路径
SceneM.lastSceneName = self.curSceneName; -- 切换到新场景
SceneM.replaceScene(self.nextScene, "crossfade", 0.2, Screen.COLOR_WHITE);
end
-- 等待一帧 不然最后一次无法绘制
performWithDelay(onComplete, CCDirector:sharedDirector():deltaTime() * );
end
end -- 清理资源
function LoadingController:cleanup( )
CCDirector:sharedDirector():getActionManager():removeAllActions();
-- 获得上一个场景配置,根据配置删除
if SceneM.lastSceneName then
local lastConfig = res[SceneM.lastSceneName];
local function removeRes( t , resType)
if t == nil or type(t) ~= "table" then
return;
end
for key, value in pairs(t) do
if (tolua.type(value) == "table") then
loopRes( value );
elseif (tolua.type(value) == "string") then
if resType == SOUNDTYPE then
-- 1.删除声音
Audio.unloadSound(value);
end if resType == TEXTURETYPE then
-- 2.删除纹理
CCTextureCache:sharedTextureCache():removeTextureForKey(value);
end if resType == ATLASTYPE then
-- 3.删除plist纹理
CCSpriteFrameCache:sharedSpriteFrameCache():removeSpriteFramesFromFile(value);
value = string.gsub(value, ".plist", ".png");
CCTextureCache:sharedTextureCache():removeTextureForKey(value);
end if resType == ANIMATIONTYPE then
-- 4.删除动画
print("删除骨骼动画资源:",value);
value = string.gsub(value, ".xml", ".png");
CCTextureCache:sharedTextureCache():removeTextureForKey(value);
value = string.gsub(value, ".png", ".plist");
CCSpriteFrameCache:sharedSpriteFrameCache():removeSpriteFramesFromFile(value);
--CCArmatureDataManager:purge();
end
end
end
end
removeRes(lastConfig.sounds, SOUNDTYPE); --删除声音资源
removeRes(lastConfig.textures, TEXTURETYPE); --删除纹理资源
removeRes(lastConfig.atlas, ATLASTYPE); --删除图集资源
removeRes(lastConfig.animations, ANIMATIONTYPE); --删除动画资源
removeRes(lastConfig.effects, ANIMATIONTYPE); --删除动画资源 -- 移除临时动态公共资源
if ResM.tempPathCache then
removeRes(ResM.tempPathCache.sounds, SOUNDTYPE); --删除声音资源
removeRes(ResM.tempPathCache.textures, TEXTURETYPE); --删除纹理资源
removeRes(ResM.tempPathCache.atlas, ATLASTYPE); --删除图集资源
removeRes(ResM.tempPathCache.animations, ANIMATIONTYPE); --删除动画资源
end
ResM.init(); CCTextureCache:sharedTextureCache():dumpCachedTextureInfo();
end -- CCSpriteFrameCache:purgeSharedSpriteFrameCache();
-- CCTextureCache:sharedTextureCache():removeAllTextures();
-- CCArmatureDataManager:purge();
end return LoadingController;

LoadingController的更多相关文章

  1. ionic LoadingController 模块使用

    html 代码: <ion-header> <ion-navbar> <ion-title>Loading</ion-title> </ion-n ...

  2. 游戏Loading中的小提示和Loading动画实现

    学习unity1年多了,工作也1年了,因为工作需要,有几个月没接触unity Ngui啦. 学的还是不踏实.继续努力吧.由于下周就要进行新游戏的开发,这几天熟悉熟悉NGUI,今天按照现在公司以前的项目 ...

  3. ionic3 Loading组件的用法

    import { LoadingController } from 'ionic-angular'; @Component({ selector: 'page-contact', templateUr ...

  4. ionic2 rc2 添加版本更新自动升级功能

    不废话,直接上代码 首先安装四个必备的插件: cordova plugin add cordova-plugin-app-version //获取APP版本 cordova plugin add co ...

  5. ionic3 Injectable 引入NavController

    在service里 引入 navcontroller 报错 And I get error No provider for NavController. 一个比较容易解决的方法, import {Io ...

  6. Unity Chan 2D Asset

    Unity Chan 2D Asset 4月份時,UNITY CHAN 官方網站推出了3D大島こはく,之後也有更新1.11版,而在六月12日時,則釋出了2D版本素材,一樣可以在UNITY CHAN 官 ...

  7. ionic 访问odoo11之具体业务类api接口

    在前面测试通过odoo登录的功能,这次的问题重点是如何访问后台具体的业务类的接口呢?这次就以我们在odoo中安装的lunch模块为例,目标是获取lunch.alert的数据,如下图 具体过程接上次文章 ...

  8. ionic访问odoo 11接口

    在架设完毕odoo 11的网站之后,第一次面临手机app该如何访问后台网站的问题,是不是模式类似asp.net mvc 那样的模式,或者还存在其他的访问方法,带着这个疑问与困惑,开始的我的研究学习之路 ...

  9. Ionic2 播放mp3功能实现

    在开发app的过程中有需要播放mp3的功能,一直想实现,但苦于具体的困难一直未能实现,经过一段时间的资料查询和测试,最终摸索出来,现记录如下: 1.最重要的是安装第三方插件ionic-audio,开源 ...

随机推荐

  1. SpringBoot中mybatis配置自动转换驼峰标识没有生效

    mybatis提供了一个配置: #开启驼峰命名转换 mybatis.configuration.map-underscore-to-camel-case=true 使用该配置可以让mybatis自动将 ...

  2. win32下编译glog

    既然编译第三方库了,google提供的VSproject是老版本的,构建不好升级.所以还是用cmake是王道. 采用out of source 编译,  以下是编译脚本bat: mkdir build ...

  3. 解决input 有多少个radio绑定change事件,手动触发就会执行多少次问题

    如题,相信大家都会遇到这个问题,那么为什么会触发多次呢?其实当你用jquery绑定onchange事件的时候你就无形中给每个radio绑定了事件,所以才会出现执行多少次的问题了,那么如何解决呢,其实这 ...

  4. ASP.NET HttpModule URL 重写 (一) 【Z】

    大家好,又来和大家见面了,此次给大家带来的URL重写,关于URL重写是什么,有什么好处,如何重写,今天我和大家一起分享一下我的经验 一.URL重写 URL重写就是首先获得一个进入的URL请求然后把它重 ...

  5. HDUOJ-----1074 Integer Inquiry

    Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. iOS 10 的一个重要更新-用 UIViewPropertyAnimator 编写动画

    曾经的黑暗年代 用基于 block 的 UIView animation 来编写 view 属性(frame, transform 等等)变化的动画非常简单.只需要短短几行代码: view.alpha ...

  7. 【转】GDB中应该知道的几个调试方法

    文章来源:http://coolshell.cn/articles/3643.html GDB中应该知道的几个调试方法 2011年02月10日 陈皓 评论 40 条评论  70,776 人阅读 七.八 ...

  8. 【Spring】spring的7个模块

    Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架. Spring ...

  9. 【Oracle】浅析Oracle中的事务

    1. 什么是事务 在数据库中事务是工作的逻辑单元,一个事务是由一个或多个完成一组的相关行为的SQL语句组成,通过事务机制确保这一组SQL语句所作的操作要么都成功执行,完成整个工作单元操作,要么一个也不 ...

  10. OpenCV 学习笔记 02 使用opencv处理图像

    1 不同色彩空间的转换 opencv 中有数百种关于不同色彩空间的转换方法,但常用的有三种色彩空间:灰度.BRG.HSV(Hue-Saturation-Value) 灰度 - 灰度色彩空间是通过去除彩 ...