cocoscreator笔记(二)DataManager
程序员文章站
2022-06-11 10:05:51
...
定义如下:
class DataMgr {
constructor() {
this.instance = null;
this.getInstance = function ()
{
if(this.instance == null)
{
this.instance = new DataMgr();
}
return this.instance;
}
this.stageData = new Array(); // 所有关卡数据容器
// 函数声明
DataMgr.prototype = {
loadAllDatas : loadAllDatas,
loadStage : loadStage,
getStageDataByID : getStageDataByID,
}
function loadAllDatas() {
this.loadStage();
}
function loadStage() {
var self = this;
cc.loader.loadRes("config/t_stage",function(err,result){
if(err != null)
{
console.log("stage mgr load error",err);
return ;
}
self.stageData = result.json;
});
}
function getStageDataByID(_id)
{
for(var idx in this.stageData)
{
if(this.stageData[idx].id == _id)
{
return JSON.parse(JSON.stringify(this.stageData[idx]));
}
}
return null;
}
}
}
module.exports = new DataMgr();
使用如下:
onLoad: function () {
DataMgr.getInstance().loadAllDatas();
},
onLoad: function () {
DataMgr.getInstance().getStageDataByID(1).enemy_group;
},
onLoad: function () {
DataMgr.getInstance().stageData[1].trajectorys;
},
其他使用:
var DataMgr = require ("./data/DataMgr")
var Global = require("Global")
cc.Class({
extends: cc.Component,
properties: {
},
onLoad: function () {
DataMgr.getInstance().loadAllDatas();
this.stageName = cc.find('center/Label_stageName');
this.stageName.getComponent(cc.Label).string = DataMgr.getInstance().getStageDataByID(Global.curStageID).stage_name;
},
update: function (dt) {
},
});
其他使用(二):
onLoad: function () {
this.temp = new Array();
this.tempGroup = new Array()
this.Imformation = DataMgr.getInstance();
for (let i = 0; i < this.Imformation.stageData.length; i++) {
this.temp = this.Imformation.stageData[i].trajectorys.split(",")
this.tempGroup.push(this.temp)
}
},