4.Cocos跑酷游戏——资源管理ResourceManager
1.List工具篇
2.工具篇 Dictionary
3.工具篇 读取Json文件保存数据
4.资源管理ResourceManager
5.界面层级管理 LayerManager
6.界面管理 UIManager
7.事件监听篇 EventBus
8.枚举篇 枚举管理
9.游戏总管 Mir
10游戏入口 Main
11.声音管理器
12首页界面
13.游戏界面
14.01 背景
15.02主角(与游戏界面交互)
16.03添加怪物来袭
17.04添加障碍物
18.05 添加障碍物排列
19.06添加奖励物品
20.07奖励物质排列数据
21.从零开始-Cocos跑酷游戏——游戏结束界面
22.最后的补充
游戏开发中的资源加载是少不了的,封装一个资源管理可以更加方便的使用
脚本名 ResourceManager.js
var ResourceManager = function(){
};
ResourceManager.m_atlasDic = null;
//加载主要界面
ResourceManager.LoadWindow = function(mName,LoadPrefabComplete){
window.cc.loader.loadRes( window.Constant.RootPath.WIND_ROOT_PATH + mName +"/" + mName,LoadPrefabComplete);
},
//加载预制体
ResourceManager.LoadPrefab = function(path,LoadPrefabComplete){
window.cc.loader.loadRes(path,LoadPrefabComplete);
},
//加载图集
ResourceManager.LoadAtlasA = function(rootPath,atlasName)
{
let atlasPath = rootPath + atlasName;
window.cc.loader.loadRes(atlasPath,cc.SpriteAtlas,function(error,list){
let spriteFrames = null;
if(error){
console.log("loadAtlas Error : "+atlasPath);
}
else
{
if(null == ResourceManager.m_atlasDic) ResourceManager.m_atlasDic = new window.dictionary();
spriteFrames = list.getSpriteFrames();
ResourceManager.m_atlasDic.add(atlasName,spriteFrames);
}
});
},
//加载图集
ResourceManager.LoadAtlas = function(atlasName,spriteObj,spriteName){
let atlasPath = window.Constant.RootPath.ATLAS_ROOT_PATH + atlasName;
window.cc.loader.loadRes(atlasPath,cc.SpriteAtlas,function(error,list){
let spriteFrames = null;
let sprite = null;
if(error){
console.log("loadAtlas Error : "+atlasPath);
}
else{
spriteFrames = list.getSpriteFrames();
ResourceManager.m_atlasDic.add(atlasName,spriteFrames);
for(var i = 0;i<spriteFrames.length;i++)
{
if(spriteName == spriteFrames[i].name)
{
sprite = spriteFrames[i];
if(spriteObj == null || spriteObj.spriteFrame == null)
{
console.log("spriteObj is null");
return;
}
spriteObj.spriteFrame = sprite;
break;
}
}
}
});
},
//atlasName,spriteName
ResourceManager.LoadSpriteFrameByName = function(spriteObj,iconPath){
var arr = iconPath.split("/");
var atlasName = arr[0];
var spriteName = arr[1];
if(ResourceManager.m_atlasDic == null)
{
ResourceManager.m_atlasDic = new window.dictionary();
}
var sprites = null;
var sprite = null;
if(!ResourceManager.m_atlasDic.containKey(atlasName))
{
ResourceManager.LoadAtlas(atlasName,spriteObj,spriteName);
return;
}
sprites = ResourceManager.m_atlasDic.get(atlasName);
for(var i = 0;i<sprites.length;i++)
{
if(spriteName == sprites[i].name)
{
sprite = sprites[i];
if(spriteObj == null || spriteObj.spriteFrame == null)
{
console.log("spriteObj is null");
return;
}
spriteObj.spriteFrame = sprite;
break;
}
}
if(null == sprite)
{
console.log("sprite is not exist" + spriteName);
}
//return sprite;
},
//根据路径加载图集
ResourceManager.LoadSpriteByPath = function(spriteObj,iconPath){
cc.loader.loadRes(iconPath, cc.SpriteFrame, function (err, spriteFrame) {
if(err)
{
console.log("加载图集出错:"+err);
}
spriteObj.spriteFrame = spriteFrame;
});
}
// ResourceManager.LoadSpriteByPath=function(spriteObj,iconPath)
// {
// cc.loader.loadRes(iconPath,cc.spriteFrame,function(err,spriteFrame))
// {
// if(err)
// {
// console.log("加载图集失败");
// return;
// }
// spriteObj.getComponent(cc.Sprite).spriteFrame
// }
// }
//加载网络资源
ResourceManager.LoadUrl = function(spriteObj,remoteUrl){
cc.loader.load({url: remoteUrl, type: 'jpg'}, function (err,texture) {
if(err)
{
console.log(err);
return;
}
var sprite = new cc.SpriteFrame(texture);
if(spriteObj == null || spriteObj.spriteFrame == null)
{
console.log("spriteObj is null");
return;
}
spriteObj.spriteFrame = sprite;
});
},
module.exports = ResourceManager;
游戏二维码
4399游戏链接:http://www.4399.com/flash/203652.htm