283 lines
7.2 KiB
JavaScript
283 lines
7.2 KiB
JavaScript
"use strict";
|
|
cc._RF.push(module, '971c4vub4BJpqrSCLPJ8g1V', 'AudioManager');
|
|
// Script/AudioManager.js
|
|
|
|
"use strict";
|
|
|
|
cc.Class({
|
|
"extends": cc.Component,
|
|
properties: {
|
|
//背景音乐
|
|
audioGameBgm0: {
|
|
"default": null,
|
|
type: cc.AudioClip
|
|
},
|
|
// 按钮点击
|
|
audioButtonClick: {
|
|
"default": null,
|
|
type: cc.AudioClip
|
|
},
|
|
// 报警音效
|
|
audioWarning: {
|
|
"default": null,
|
|
type: cc.AudioClip
|
|
},
|
|
//游戏开始音效
|
|
audioWin: {
|
|
"default": null,
|
|
type: cc.AudioClip
|
|
},
|
|
reward: false,
|
|
finish: false,
|
|
rewardCount: 0
|
|
},
|
|
ctor: function ctor() {
|
|
this.mAudioMap = {};
|
|
/**
|
|
* 默认音量大小
|
|
* @type {number}
|
|
*/
|
|
|
|
this.bgMusicVolume = 0.1;
|
|
this.effectMusicVolume = 1;
|
|
this.mMusicSwitch = 1;
|
|
this.mEffectSwitch = 1;
|
|
},
|
|
play: function play(audioSource, loop, callback, isBgMusic) {
|
|
if (isBgMusic && !this.mMusicSwitch) return;
|
|
if (!isBgMusic && !this.mEffectSwitch) return;
|
|
var volume = isBgMusic ? this.bgMusicVolume : this.effectMusicVolume;
|
|
|
|
if (cc.sys.isBrowser) {
|
|
if (audioSource == this.brickSound) {
|
|
volume = 0.1;
|
|
}
|
|
|
|
var context = cc.audioEngine.play(audioSource, loop, volume);
|
|
|
|
if (callback) {
|
|
cc.audioEngine.setFinishCallback(context, function () {
|
|
callback.call(this);
|
|
}.bind(this));
|
|
}
|
|
|
|
this.mAudioMap[audioSource] = context;
|
|
return audioSource;
|
|
} else {
|
|
var context = wx.createInnerAudioContext();
|
|
context.autoplay = true;
|
|
context.loop = loop;
|
|
context.obeyMuteSwitch = true;
|
|
context.volume = volume;
|
|
|
|
if (callback) {
|
|
context.onEnded(function () {
|
|
callback.call(this);
|
|
}.bind(this));
|
|
} else {
|
|
context.offEnded();
|
|
} // var audioPath = cc.url.raw("resources/BallMaster/sounds" + audioSource.name + ".mp3");
|
|
|
|
|
|
context.src = audioSource.nativeUrl;
|
|
context.play();
|
|
this.mAudioMap[audioSource] = context;
|
|
return audioSource;
|
|
}
|
|
},
|
|
save: function save() {// cc.wwx.Storage.setItem(cc.wwx.Storage.Key_Setting_Music_Volume, this.mMusicSwitch);
|
|
// cc.wwx.Storage.setItem(cc.wwx.Storage.Key_Setting_Effect_Volume, this.mEffectSwitch);
|
|
},
|
|
onLoad: function onLoad() {
|
|
var _this = this;
|
|
|
|
// cc.wwx.Storage.getItem(cc.wwx.Storage.Key_Setting_Music_Volume, function(volume) {
|
|
// this.mMusicSwitch = parseInt(volume);
|
|
// }.bind(this), 1);
|
|
// cc.wwx.Storage.getItem(cc.wwx.Storage.Key_Setting_Effect_Volume, function(volume) {
|
|
// this.mEffectSwitch = parseInt(volume);
|
|
// }.bind(this), 1);
|
|
this.reward = false;
|
|
this.finish = false;
|
|
this.rewardCount = 0;
|
|
window.addEventListener('rewardCanUse', function () {
|
|
// 此时激励可用 游戏内部逻辑
|
|
_this.reward = true;
|
|
});
|
|
'undefined' != typeof window['reward'] ? window.dispatchEvent(window['reward']) : "";
|
|
this.preload();
|
|
},
|
|
preload: function preload() {
|
|
if (!(cc.sys.platform === cc.sys.WECHAT_GAME)) {
|
|
return;
|
|
}
|
|
|
|
var musics = [this.audioGameBgm0];
|
|
musics.forEach(function (path) {// var musicPath = wxDownloader.REMOTE_SERVER_ROOT + path;
|
|
// if (musicPath != wxDownloader.REMOTE_SERVER_ROOT && musicPath.endsWith('.mp3')) {
|
|
// cc.loader.load(musicPath, function(err, remoteUrl) {
|
|
// if (err) {
|
|
// cc.error(err.message || err);
|
|
// return;
|
|
// }
|
|
// });
|
|
// }
|
|
});
|
|
},
|
|
getAudioMusicSwitch: function getAudioMusicSwitch() {
|
|
return this.mMusicSwitch;
|
|
},
|
|
getAudioEffectSwitch: function getAudioEffectSwitch() {
|
|
return this.mEffectSwitch;
|
|
},
|
|
trunAudioSound: function trunAudioSound(on) {
|
|
this.switchMusic(on);
|
|
this.switchEffect(on);
|
|
},
|
|
switchMusic: function switchMusic(on) {
|
|
if (this.mMusicSwitch != (on ? 1 : 0)) {
|
|
this.mMusicSwitch = 1 - this.mMusicSwitch; // this.save();
|
|
}
|
|
|
|
if (on) {
|
|
this.playMusicGame();
|
|
} else {
|
|
this.stopMusic();
|
|
}
|
|
},
|
|
switchEffect: function switchEffect(on) {
|
|
if (this.mEffectSwitch != (on ? 1 : 0)) {
|
|
this.mEffectSwitch = 1 - this.mEffectSwitch; // this.save();
|
|
}
|
|
},
|
|
onHide: function onHide() {
|
|
cc.audioEngine.pauseAll(); // if (CC_JSB) {
|
|
// } else {
|
|
// for (var key in this.mAudioMap) {
|
|
// if (key === this.mMusicKey) {
|
|
// this.mAudioMap[key].pause();
|
|
// } else {
|
|
// this.mAudioMap[key].stop();
|
|
// }
|
|
// }
|
|
// }
|
|
},
|
|
onShow: function onShow() {
|
|
cc.audioEngine.resumeAll(); // if (CC_JSB) {
|
|
// } else {
|
|
// if (!this.mMusicSwitch) return;
|
|
// var context = this.mAudioMap[this.mMusicKey];
|
|
// if (context) {
|
|
// context.play();
|
|
// }
|
|
// }
|
|
},
|
|
playMusic: function playMusic(key, callback, loop) {
|
|
loop = typeof loop == 'undefined' || loop ? true : false;
|
|
this.stopMusic();
|
|
this.mMusicKey = this.play(key, loop, callback, true);
|
|
},
|
|
|
|
/**
|
|
* 游戏背景音乐
|
|
*/
|
|
playMusicGame: function playMusicGame() {
|
|
this.playMusic(this.audioGameBgm0);
|
|
},
|
|
|
|
/**
|
|
* 停止背景音乐播放
|
|
*/
|
|
stopMusic: function stopMusic() {
|
|
// cc.wwx.OutPut.log('stopMusic audio effect wx: ' + this.mMusicKey);
|
|
var context = this.mAudioMap[this.mMusicKey];
|
|
|
|
if (typeof context != 'undefined') {
|
|
if (cc.sys.isBrowser) {
|
|
cc.audioEngine.stop(context);
|
|
} else {
|
|
context.stop();
|
|
}
|
|
|
|
cc.audioEngine.stop(context);
|
|
}
|
|
},
|
|
// 炸弹、火箭爆炸音效
|
|
playBomb: function playBomb() {
|
|
return this.play(this.audioBomb, false);
|
|
},
|
|
//激光音效
|
|
playJiGuang: function playJiGuang() {
|
|
return this.play(this.audioJiGuang, false);
|
|
},
|
|
//技能音效
|
|
playItem1: function playItem1() {
|
|
return this.play(this.audioItem1, false);
|
|
},
|
|
|
|
/*
|
|
* 游戏开始音效
|
|
*
|
|
*/
|
|
playGameStart: function playGameStart() {
|
|
return this.play(this.audioGameStart, false);
|
|
},
|
|
|
|
/*
|
|
* 失败的游戏结束
|
|
*/
|
|
playGameOver: function playGameOver() {
|
|
return this.play(this.audioGameOver, false);
|
|
},
|
|
|
|
/*
|
|
* 成功的游戏结束
|
|
*/
|
|
playGameResultFailed: function playGameResultFailed() {
|
|
return this.play(this.audioGameResultFail, false);
|
|
},
|
|
|
|
/*
|
|
* 成功的游戏结束
|
|
*/
|
|
playGameResultSuccess: function playGameResultSuccess() {
|
|
return this.play(this.audioGameResultSuccess, false);
|
|
},
|
|
|
|
/**
|
|
* 报警的音效
|
|
*/
|
|
playWarning: function playWarning() {
|
|
return this.play(this.audioWarning, false);
|
|
},
|
|
playWin: function playWin() {
|
|
return this.play(this.audioWin, false);
|
|
},
|
|
|
|
/*
|
|
* 方块碰撞的声音
|
|
*/
|
|
playBrick: function playBrick() {
|
|
return this.play(this.brickSound, false);
|
|
},
|
|
//方块破碎的声音
|
|
brickBoom: function brickBoom() {
|
|
return this.play(this.audioObj, false);
|
|
},
|
|
|
|
/**
|
|
* 按钮
|
|
*/
|
|
playAudioButton: function playAudioButton() {
|
|
return this.play(this.audioButtonClick, false);
|
|
},
|
|
playRandomMatch: function playRandomMatch() {
|
|
this.playMusic(this.randomMatchSound);
|
|
},
|
|
playMatchFoundSound: function playMatchFoundSound() {
|
|
this.stopMusic();
|
|
return this.play(this.matchFoundSound, false);
|
|
}
|
|
});
|
|
|
|
cc._RF.pop(); |