From ee2491adb4c13e617d6817790ee03f5d989dff1b Mon Sep 17 00:00:00 2001 From: wally <18603454788@163.com> Date: Thu, 30 Sep 2021 09:48:32 +0800 Subject: [PATCH] fix: bgm --- src/classes/sound.js | 66 +++++++++++++++++++++----------------------- src/custom.js | 12 ++++++-- src/test.js | 1 - 3 files changed, 41 insertions(+), 38 deletions(-) diff --git a/src/classes/sound.js b/src/classes/sound.js index 21f8ec5..9c61f44 100644 --- a/src/classes/sound.js +++ b/src/classes/sound.js @@ -1,19 +1,36 @@ -// 音频处理类 -// function Sound() { -// this.lib = library; +// 音频处理类; +function Sound() { + this.lib = library; -// this.music = {}; -// } + this.music = {}; +} + +Sound.of = function () { + const instance = new Sound(); + instance.init(); + return instance; +}; + +Sound.prototype.init = function () { + createjs.Sound.alternateExtensions = ['mp3']; + console.log('createjs.Sound: ', createjs.Sound); + const sounds = [{ src: 'sounds/bgmMusic.mp3', id: 'bgm' }]; + const _this = this; + createjs.Sound.addEventListener('fileload', function (event) { + console.log('event: ', event); + _this.music[event.id] = createjs.Sound.createInstance(event.id); + }); + createjs.Sound.registerSounds(sounds, './'); + this.playBgm(); +}; -// Sound.of = function () { -// const instance = new Sound(); -// instance.init(); -// return instance; -// }; +Sound.prototype.playBgm = function () { + this.music.bgm && this.music.bgm.play({ loop: -1, volume: 0.3 }); // 播放背景音乐 +}; -// Sound.prototype.init = function () { +// let music = {}; +// function initSound() { // createjs.Sound.alternateExtensions = ['mp3']; -// console.log('createjs.Sound: ', createjs.Sound); // const sounds = [ // { src: 'public/sounds/bgmMusic.mp3', id: 'bgm' }, // { src: 'public/sounds/excitationMusic.mp3', id: 'excitation' }, @@ -21,28 +38,7 @@ // { src: 'public/sounds/unbelievable.mp3', id: 'unbelievable' }, // ]; // createjs.Sound.addEventListener('fileload', function (event) { -// console.log('event: ', event); -// this.music[event.id] = createjs.Sound.createInstance(event.id); +// music[event.id] = createjs.Sound.createInstance(event.id); // }); // createjs.Sound.registerSounds(sounds, '../../'); -// this.playBgm(); -// }; - -// Sound.prototype.playBgm = function () { -// this.music.bgm && this.music.bgm.play({ loop: -1, volume: 0.3 }); // 播放背景音乐 -// }; - -let music = {}; -function initSound() { - createjs.Sound.alternateExtensions = ['mp3']; - const sounds = [ - { src: 'public/sounds/bgmMusic.mp3', id: 'bgm' }, - { src: 'public/sounds/excitationMusic.mp3', id: 'excitation' }, - { src: 'public/sounds/amazing.mp3', id: 'amazing' }, - { src: 'public/sounds/unbelievable.mp3', id: 'unbelievable' }, - ]; - createjs.Sound.addEventListener('fileload', function (event) { - music[event.id] = createjs.Sound.createInstance(event.id); - }); - createjs.Sound.registerSounds(sounds, '../../'); -} +// } diff --git a/src/custom.js b/src/custom.js index 66e95a7..0b98002 100644 --- a/src/custom.js +++ b/src/custom.js @@ -22,13 +22,21 @@ function initStage(lib) { window.main = Main.of(gameOver); // 初始化鸟等 window.timeInstance = Time.of(gameOver); // 初始化游戏时间 - // window.soundInstance = Sound.of(); // 初始化音频 - initSound(); // 播放背景音乐 + window.soundInstance = Sound.of(); // 初始化音频 Level.of(2); // 游戏难度级别 Back.of(); // 返回按钮 isHash(); window.suspend = Suspend.of(); + + window.addEventListener( + 'click', + function () { + if (!window.soundInstance) return; + window.soundInstance.playBgm(); + }, + false, + ); } // 游戏结束 显示结束得分面板 diff --git a/src/test.js b/src/test.js index 5b69b21..3ce6bfa 100644 --- a/src/test.js +++ b/src/test.js @@ -1,6 +1,5 @@ // 判断是否在演示模式 能否直接开始游戏 function isHash() { - initSound; if (location.hash && location.hash === '#p') { isDemo = true; Demo.of();