From f9133aff92a64ad990aa4880029837808dd58977 Mon Sep 17 00:00:00 2001 From: oc Date: Mon, 17 Dec 2018 00:40:26 +0800 Subject: [PATCH] Cache bgms --- libs/loader.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/libs/loader.js b/libs/loader.js index 03b97e4b..a513ecae 100644 --- a/libs/loader.js +++ b/libs/loader.js @@ -272,12 +272,19 @@ loader.prototype.freeBgm = function (name) { loader.prototype.loadBgm = function (name) { if (!core.isset(core.material.bgms[name])) return; - // 预加载BGM - core.material.bgms[name].load(); - // 移动到缓存最前方 - core.musicStatus.cachedBgms = core.musicStatus.cachedBgms.filter(function (t) {return t!=name; }); - core.musicStatus.cachedBgms.unshift(name); - if (core.musicStatus.cachedBgms.length > core.musicStatus.cachedBgmCount) { - this.freeBgm(core.musicStatus.cachedBgms.pop()); + // 是否已经预加载过 + var index = core.musicStatus.cachedBgms.indexOf(name); + if (index>=0) { + core.musicStatus.cachedBgms.splice(index, 1); } + else { + // 预加载BGM + core.material.bgms[name].load(); + // 清理尾巴 + if (core.musicStatus.cachedBgms.length == core.musicStatus.cachedBgmCount) { + this.freeBgm(core.musicStatus.cachedBgms.pop()); + } + } + // 移动到缓存最前方 + core.musicStatus.cachedBgms.unshift(name); } \ No newline at end of file