Commit 50583886 authored by 范雪寒's avatar 范雪寒

feat: global变量问题

parent d51018ee
......@@ -13,6 +13,7 @@ var game = cc.Class({
},
ctor: function () {
initGlobal_NGT_02();
game.inst = this;
g.game = game;
},
......
// 全局环境预声明
window.g = window.g || {}; // 全局
console.log('window.g init');
\ No newline at end of file
function initGlobal_NGT_02() {
console.log('window.g init');
window.g = {};
initDataMgr_NGT_02();
initEventMgr_NGT_02();
initStorageMgr_NGT_02();
initResMgr_NGT_02();
initSndMgr_NGT_02();
}
\ No newline at end of file
This diff is collapsed.
// 事件汞
let eventList = {}; // 响应列表(元素结构:eventName,[[target:cb]])
g.event_mgr = {
// 注册事件-响应 入参:事件名、响应、目标名
reg: function (eventName, cb, target) {
var event = eventList[eventName];
if (!event) {
event = eventList[eventName] = {};
}
event[target] = cb;
},
// 注销事件-响应 入参:事件名、目标名
unReg: function (eventName, target) {
var event = eventList[eventName];
if (event) {
if (event[target]) {
event[target] = null;
}
}
},
unRegName: function (eventName) {
eventList[eventName] = {};
},
// 广播事件 入参:事件名、参数
send: function (eventName, params) {
var event = eventList[eventName];
if (event) {
for (var target in event) {
var cb = event[target];
if (cb) {
cb(params);
}
}
}
},
getReglist: function () {
return eventList;
}
};
\ No newline at end of file
function initEventMgr_NGT_02() {
// 事件汞
let eventList = {}; // 响应列表(元素结构:eventName,[[target:cb]])
g.event_mgr = {
// 注册事件-响应 入参:事件名、响应、目标名
reg: function (eventName, cb, target) {
var event = eventList[eventName];
if (!event) {
event = eventList[eventName] = {};
}
event[target] = cb;
},
// 注销事件-响应 入参:事件名、目标名
unReg: function (eventName, target) {
var event = eventList[eventName];
if (event) {
if (event[target]) {
event[target] = null;
}
}
},
unRegName: function (eventName) {
eventList[eventName] = {};
},
// 广播事件 入参:事件名、参数
send: function (eventName, params) {
var event = eventList[eventName];
if (event) {
for (var target in event) {
var cb = event[target];
if (cb) {
cb(params);
}
}
}
},
getReglist: function () {
return eventList;
},
};
}
// localStorage封装
g.local_storage = {
function initStorageMgr_NGT_02() {
// localStorage封装
g.local_storage = {
// 背景音乐音量
getMusicVolume: function () {
var vol = cc.sys.localStorage.getItem("music");
return vol;
var vol = cc.sys.localStorage.getItem("music");
return vol;
},
setMusicVolume: function (vol) {
cc.sys.localStorage.setItem('music', vol)
cc.sys.localStorage.setItem("music", vol);
},
// 音效音量
getEffectsVolume: function () {
var vol = cc.sys.localStorage.getItem("effect");
return vol;
var vol = cc.sys.localStorage.getItem("effect");
return vol;
},
setEffectsVolume: function (vol) {
cc.sys.localStorage.setItem('effect', vol);
cc.sys.localStorage.setItem("effect", vol);
},
};
};
}
/**
* 资源管理器
*/
g.res_mgr = {
//获得表数据数据
getFormData() {
console.log('初始化数据');
if (window && window.courseware) {
window.courseware.getData((res) => {
//存入数据管理器
g.data_mgr.data = res;
//数据处理
g.data_mgr.proGameData();
console.log("获得表单数据:" + res);
});
return;
}
function initResMgr_NGT_02() {
/**
* 资源管理器
*/
g.res_mgr = {
//获得表数据数据
getFormData() {
console.log("初始化数据");
const middleLayer = cc.find('middleLayer');
if (middleLayer) {
const middleLayerComponent = middleLayer.getComponent('middleLayer');
middleLayerComponent.getData((res) => {
//存入数据管理器
g.data_mgr.data = res;
//数据处理
g.data_mgr.proGameData();
console.log("获得表单数据:" + res);
});
return;
}
if (window && window.courseware) {
window.courseware.getData((res) => {
//存入数据管理器
g.data_mgr.data = res;
//数据处理
g.data_mgr.proGameData();
console.log("获得表单数据:" + res);
});
return;
}
//console.error('没有查找到courseware.getData方法', error);
//获得默认数据
g.data_mgr.data = g.data_mgr.getDefaultData();
//数据处理
g.data_mgr.proGameData();
},
const middleLayer = cc.find("middleLayer");
if (middleLayer) {
const middleLayerComponent = middleLayer.getComponent("middleLayer");
middleLayerComponent.getData((res) => {
//存入数据管理器
g.data_mgr.data = res;
//数据处理
g.data_mgr.proGameData();
console.log("获得表单数据:" + res);
});
return;
}
//得到图片资源
getSpriteFrimeByUrl(url, cb) {
cc.assetManager.loadRemote(url, cc.SpriteFrame, (e, sp) => {
const spriteFrame = new cc.SpriteFrame(sp)
cb && cb(spriteFrame);
});
},
//console.error('没有查找到courseware.getData方法', error);
//获得默认数据
g.data_mgr.data = g.data_mgr.getDefaultData();
//数据处理
g.data_mgr.proGameData();
},
playAudioByUrl(audio_url, cb) {
if (audio_url) {
cc.assetManager.loadRemote(audio_url, (err, audioClip) => {
cb && cb(audioClip);
});
}
},
//得到图片资源
getSpriteFrimeByUrl(url, cb) {
cc.assetManager.loadRemote(url, cc.SpriteFrame, (e, sp) => {
const spriteFrame = new cc.SpriteFrame(sp);
cb && cb(spriteFrame);
});
},
//加载龙骨
loadSpine(animationDisplay, Info) {
if (Info.type == 'Image') {
return;
}
cc.assetManager.loadAny([{ url: Info.tex_json, ext: '.txt' }, { url: Info.ske_json, ext: '.txt' }], (error, assets) => {
if (error) {
console.log(error)
}
else {
cc.assetManager.loadRemote(Info.tex_png, (error, texture) => {
if (error) {
console.log(error)
}
else {
var atlas = new dragonBones.DragonBonesAtlasAsset();
atlas._uuid = Info.tex_json;
atlas.atlasJson = assets[0];
atlas.texture = texture;
playAudioByUrl(audio_url, cb) {
if (audio_url) {
cc.assetManager.loadRemote(audio_url, (err, audioClip) => {
cb && cb(audioClip);
});
}
},
var asset = new dragonBones.DragonBonesAsset();
asset._uuid = Info.ske_json;
asset.dragonBonesJson = assets[1];
//加载龙骨
loadSpine(animationDisplay, Info) {
if (Info.type == "Image") {
return;
}
cc.assetManager.loadAny(
[
{ url: Info.tex_json, ext: ".txt" },
{ url: Info.ske_json, ext: ".txt" },
],
(error, assets) => {
if (error) {
console.log(error);
} else {
cc.assetManager.loadRemote(Info.tex_png, (error, texture) => {
if (error) {
console.log(error);
} else {
var atlas = new dragonBones.DragonBonesAtlasAsset();
atlas._uuid = Info.tex_json;
atlas.atlasJson = assets[0];
atlas.texture = texture;
animationDisplay.dragonAtlasAsset = atlas;
animationDisplay.dragonAsset = asset;
let data = asset._dragonBonesJsonData.armature[0];
if (!data) {
return;
}
animationDisplay.armatureName = data.name;
g.data_mgr.dragonName = data.animation[0].name;
var asset = new dragonBones.DragonBonesAsset();
asset._uuid = Info.ske_json;
asset.dragonBonesJson = assets[1];
animationDisplay.node.active = true;
}
});
}
});
},
}
\ No newline at end of file
animationDisplay.dragonAtlasAsset = atlas;
animationDisplay.dragonAsset = asset;
let data = asset._dragonBonesJsonData.armature[0];
if (!data) {
return;
}
animationDisplay.armatureName = data.name;
g.data_mgr.dragonName = data.animation[0].name;
animationDisplay.node.active = true;
}
});
}
}
);
},
};
}
// 声音管理器
g.snd_mgr = {
bgmId: -1, // 背景音乐的音频ID
effIds: [], // 音效的音频ID列表(由cc.audioEngine保证音频ID不重复)
bgmVol: 1, // 背景音乐音量
neweffId: null, //保存上一个音效ID
effVol: 1, // 音效音量
pausebgVol: 1,
pauseeffVol: 1,
sndNativeUrls: {},
newsnd: null,
init: function () {
var local_storage = g.local_storage;
var music_vol = local_storage.getMusicVolume();
var effect_vol = local_storage.getEffectsVolume();
music_vol != undefined && music_vol + "" != "" && this.setMusicVolume(music_vol);
effect_vol != undefined && effect_vol + "" != "" && this.setEffectsVolume(effect_vol);
},
delAudId: function (id) {
if (id == this.bgmId) {
this.bgmId = -1;
return;
}
for (var i = 0; i < this.effIds.length; ++i) {
if (this.effIds[i] == id) {
this.effIds.splice(i, 1);
return;
}
}
},
playMusic: function (snd, _loop, finishCB) {
if (!snd) return;
this.newsnd = snd;
var loop = _loop ? false : true; // 除非指定为false,否则默认为true
// if (g.configs.platform == "vo") {
// this.bgmId = cc.audioEngine.play(snd, loop);
// return;
// }
cc.audioEngine.stop()
this.bgmId = cc.audioEngine.playMusic(snd, loop);
// 播放完成回调
if (finishCB) {
cc.audioEngine.setFinishCallback(this.bgmId, function () {
finishCB();
});
}
},
playEffect: function (snd, finishCB) {
if (!snd || this.effVol == 0) return;
var id = cc.audioEngine.playEffect(snd, false); // 音效限定不能重复播放
this.playaudioEffect(id, finishCB);
},
//播放音效
playaudioEffect: function (id, finishCB) {
var self = this;
this.neweffId = id;
this.effIds.push(id);
// 播放完记得删ID
cc.audioEngine.setFinishCallback(id, function () {
self.delAudId(id);
finishCB && finishCB();
});
},
pauseVolume: function () {
cc.audioEngine.stopAll();
},
resumeVolume: function () {
if (this.newsnd != null) {
cc.audioEngine.playMusic(this.newsnd)
}
},
setMusicVolume: function (percent) {
this.bgmVol = percent;
cc.audioEngine.setMusicVolume(~~percent);
},
setEffectsVolume: function (percent) {
this.effVol = percent;
cc.audioEngine.setEffectsVolume(~~percent);
cc.audioEngine.setMusicVolume(~~this.bgmVol);
},
};
\ No newline at end of file
function initSndMgr_NGT_02() {
// 声音管理器
g.snd_mgr = {
bgmId: -1, // 背景音乐的音频ID
effIds: [], // 音效的音频ID列表(由cc.audioEngine保证音频ID不重复)
bgmVol: 1, // 背景音乐音量
neweffId: null, //保存上一个音效ID
effVol: 1, // 音效音量
pausebgVol: 1,
pauseeffVol: 1,
sndNativeUrls: {},
newsnd: null,
init: function () {
var local_storage = g.local_storage;
var music_vol = local_storage.getMusicVolume();
var effect_vol = local_storage.getEffectsVolume();
music_vol != undefined &&
music_vol + "" != "" &&
this.setMusicVolume(music_vol);
effect_vol != undefined &&
effect_vol + "" != "" &&
this.setEffectsVolume(effect_vol);
},
delAudId: function (id) {
if (id == this.bgmId) {
this.bgmId = -1;
return;
}
for (var i = 0; i < this.effIds.length; ++i) {
if (this.effIds[i] == id) {
this.effIds.splice(i, 1);
return;
}
}
},
playMusic: function (snd, _loop, finishCB) {
if (!snd) return;
this.newsnd = snd;
var loop = _loop ? false : true; // 除非指定为false,否则默认为true
// if (g.configs.platform == "vo") {
// this.bgmId = cc.audioEngine.play(snd, loop);
// return;
// }
cc.audioEngine.stop();
this.bgmId = cc.audioEngine.playMusic(snd, loop);
// 播放完成回调
if (finishCB) {
cc.audioEngine.setFinishCallback(this.bgmId, function () {
finishCB();
});
}
},
playEffect: function (snd, finishCB) {
if (!snd || this.effVol == 0) return;
var id = cc.audioEngine.playEffect(snd, false); // 音效限定不能重复播放
this.playaudioEffect(id, finishCB);
},
//播放音效
playaudioEffect: function (id, finishCB) {
var self = this;
this.neweffId = id;
this.effIds.push(id);
// 播放完记得删ID
cc.audioEngine.setFinishCallback(id, function () {
self.delAudId(id);
finishCB && finishCB();
});
},
pauseVolume: function () {
cc.audioEngine.stopAll();
},
resumeVolume: function () {
if (this.newsnd != null) {
cc.audioEngine.playMusic(this.newsnd);
}
},
setMusicVolume: function (percent) {
this.bgmVol = percent;
cc.audioEngine.setMusicVolume(~~percent);
},
setEffectsVolume: function (percent) {
this.effVol = percent;
cc.audioEngine.setEffectsVolume(~~percent);
cc.audioEngine.setMusicVolume(~~this.bgmVol);
},
};
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment