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

feat: global变量问题

parent d51018ee
...@@ -13,6 +13,7 @@ var game = cc.Class({ ...@@ -13,6 +13,7 @@ var game = cc.Class({
}, },
ctor: function () { ctor: function () {
initGlobal_NGT_02();
game.inst = this; game.inst = this;
g.game = game; g.game = game;
}, },
......
// 全局环境预声明 // 全局环境预声明
window.g = window.g || {}; // 全局 function initGlobal_NGT_02() {
console.log('window.g init');
console.log('window.g init'); window.g = {};
\ No newline at end of file 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.
// 事件汞 function initEventMgr_NGT_02() {
let eventList = {}; // 响应列表(元素结构:eventName,[[target:cb]]) // 事件汞
g.event_mgr = { let eventList = {}; // 响应列表(元素结构:eventName,[[target:cb]])
g.event_mgr = {
// 注册事件-响应 入参:事件名、响应、目标名 // 注册事件-响应 入参:事件名、响应、目标名
reg: function (eventName, cb, target) { reg: function (eventName, cb, target) {
var event = eventList[eventName]; var event = eventList[eventName];
...@@ -35,5 +36,6 @@ g.event_mgr = { ...@@ -35,5 +36,6 @@ g.event_mgr = {
}, },
getReglist: function () { getReglist: function () {
return eventList; return eventList;
} },
}; };
\ No newline at end of file }
// localStorage封装 function initStorageMgr_NGT_02() {
g.local_storage = { // localStorage封装
g.local_storage = {
// 背景音乐音量 // 背景音乐音量
getMusicVolume: function () { getMusicVolume: function () {
var vol = cc.sys.localStorage.getItem("music"); var vol = cc.sys.localStorage.getItem("music");
return vol; return vol;
}, },
setMusicVolume: function (vol) { setMusicVolume: function (vol) {
cc.sys.localStorage.setItem('music', vol) cc.sys.localStorage.setItem("music", vol);
}, },
// 音效音量 // 音效音量
...@@ -15,6 +16,7 @@ g.local_storage = { ...@@ -15,6 +16,7 @@ g.local_storage = {
return vol; return vol;
}, },
setEffectsVolume: function (vol) { setEffectsVolume: function (vol) {
cc.sys.localStorage.setItem('effect', vol); cc.sys.localStorage.setItem("effect", vol);
}, },
}; };
}
/** function initResMgr_NGT_02() {
/**
* 资源管理器 * 资源管理器
*/ */
g.res_mgr = { g.res_mgr = {
//获得表数据数据 //获得表数据数据
getFormData() { getFormData() {
console.log('初始化数据'); console.log("初始化数据");
if (window && window.courseware) { if (window && window.courseware) {
window.courseware.getData((res) => { window.courseware.getData((res) => {
...@@ -17,9 +18,9 @@ g.res_mgr = { ...@@ -17,9 +18,9 @@ g.res_mgr = {
return; return;
} }
const middleLayer = cc.find('middleLayer'); const middleLayer = cc.find("middleLayer");
if (middleLayer) { if (middleLayer) {
const middleLayerComponent = middleLayer.getComponent('middleLayer'); const middleLayerComponent = middleLayer.getComponent("middleLayer");
middleLayerComponent.getData((res) => { middleLayerComponent.getData((res) => {
//存入数据管理器 //存入数据管理器
g.data_mgr.data = res; g.data_mgr.data = res;
...@@ -40,7 +41,7 @@ g.res_mgr = { ...@@ -40,7 +41,7 @@ g.res_mgr = {
//得到图片资源 //得到图片资源
getSpriteFrimeByUrl(url, cb) { getSpriteFrimeByUrl(url, cb) {
cc.assetManager.loadRemote(url, cc.SpriteFrame, (e, sp) => { cc.assetManager.loadRemote(url, cc.SpriteFrame, (e, sp) => {
const spriteFrame = new cc.SpriteFrame(sp) const spriteFrame = new cc.SpriteFrame(sp);
cb && cb(spriteFrame); cb && cb(spriteFrame);
}); });
}, },
...@@ -55,19 +56,22 @@ g.res_mgr = { ...@@ -55,19 +56,22 @@ g.res_mgr = {
//加载龙骨 //加载龙骨
loadSpine(animationDisplay, Info) { loadSpine(animationDisplay, Info) {
if (Info.type == 'Image') { if (Info.type == "Image") {
return; return;
} }
cc.assetManager.loadAny([{ url: Info.tex_json, ext: '.txt' }, { url: Info.ske_json, ext: '.txt' }], (error, assets) => { cc.assetManager.loadAny(
[
{ url: Info.tex_json, ext: ".txt" },
{ url: Info.ske_json, ext: ".txt" },
],
(error, assets) => {
if (error) { if (error) {
console.log(error) console.log(error);
} } else {
else {
cc.assetManager.loadRemote(Info.tex_png, (error, texture) => { cc.assetManager.loadRemote(Info.tex_png, (error, texture) => {
if (error) { if (error) {
console.log(error) console.log(error);
} } else {
else {
var atlas = new dragonBones.DragonBonesAtlasAsset(); var atlas = new dragonBones.DragonBonesAtlasAsset();
atlas._uuid = Info.tex_json; atlas._uuid = Info.tex_json;
atlas.atlasJson = assets[0]; atlas.atlasJson = assets[0];
...@@ -90,6 +94,8 @@ g.res_mgr = { ...@@ -90,6 +94,8 @@ g.res_mgr = {
} }
}); });
} }
}); }
);
}, },
};
} }
// 声音管理器 function initSndMgr_NGT_02() {
g.snd_mgr = { // 声音管理器
g.snd_mgr = {
bgmId: -1, // 背景音乐的音频ID bgmId: -1, // 背景音乐的音频ID
effIds: [], // 音效的音频ID列表(由cc.audioEngine保证音频ID不重复) effIds: [], // 音效的音频ID列表(由cc.audioEngine保证音频ID不重复)
bgmVol: 1, // 背景音乐音量 bgmVol: 1, // 背景音乐音量
...@@ -13,8 +14,12 @@ g.snd_mgr = { ...@@ -13,8 +14,12 @@ g.snd_mgr = {
var local_storage = g.local_storage; var local_storage = g.local_storage;
var music_vol = local_storage.getMusicVolume(); var music_vol = local_storage.getMusicVolume();
var effect_vol = local_storage.getEffectsVolume(); var effect_vol = local_storage.getEffectsVolume();
music_vol != undefined && music_vol + "" != "" && this.setMusicVolume(music_vol); music_vol != undefined &&
effect_vol != undefined && effect_vol + "" != "" && this.setEffectsVolume(effect_vol); music_vol + "" != "" &&
this.setMusicVolume(music_vol);
effect_vol != undefined &&
effect_vol + "" != "" &&
this.setEffectsVolume(effect_vol);
}, },
delAudId: function (id) { delAudId: function (id) {
if (id == this.bgmId) { if (id == this.bgmId) {
...@@ -36,7 +41,7 @@ g.snd_mgr = { ...@@ -36,7 +41,7 @@ g.snd_mgr = {
// this.bgmId = cc.audioEngine.play(snd, loop); // this.bgmId = cc.audioEngine.play(snd, loop);
// return; // return;
// } // }
cc.audioEngine.stop() cc.audioEngine.stop();
this.bgmId = cc.audioEngine.playMusic(snd, loop); this.bgmId = cc.audioEngine.playMusic(snd, loop);
// 播放完成回调 // 播放完成回调
if (finishCB) { if (finishCB) {
...@@ -66,7 +71,7 @@ g.snd_mgr = { ...@@ -66,7 +71,7 @@ g.snd_mgr = {
}, },
resumeVolume: function () { resumeVolume: function () {
if (this.newsnd != null) { if (this.newsnd != null) {
cc.audioEngine.playMusic(this.newsnd) cc.audioEngine.playMusic(this.newsnd);
} }
}, },
setMusicVolume: function (percent) { setMusicVolume: function (percent) {
...@@ -78,4 +83,5 @@ g.snd_mgr = { ...@@ -78,4 +83,5 @@ g.snd_mgr = {
cc.audioEngine.setEffectsVolume(~~percent); cc.audioEngine.setEffectsVolume(~~percent);
cc.audioEngine.setMusicVolume(~~this.bgmVol); cc.audioEngine.setMusicVolume(~~this.bgmVol);
}, },
}; };
\ No newline at end of file }
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