123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- const GameModule = require("../utils/GameModule");
- cc.Class({
- extends: cc.Component,
- properties: {
- bgm: {
- tooltip: '背景音乐',
- default: null,
- type: cc.AudioClip
- },
- buttonClickAudio: {
- tooltip: '普通按钮点击音效',
- default: null,
- type: cc.AudioClip
- },
- getAwardAudio: {
- tooltip: '获得技能、奖励音效',
- default: null,
- type: cc.AudioClip
- },
- updateBuildingAudio: {
- tooltip: '升级或解锁建筑,购买明星音效',
- default: null,
- type: cc.AudioClip
- },
- clickCatAudio: {
- tooltip: '点击招财猫音效',
- default: null,
- type: cc.AudioClip
- },
- getDrawAudio: {
- tooltip: '抽奖音效',
- default: null,
- type: cc.AudioClip
- },
- getStarAudio: {
- tooltip: '获得明星音效',
- default: null,
- type: cc.AudioClip
- },
- volume: {
- tooltip: '总音量',
- default: 1,
- type: cc.Integer
- },
- bgmOpen: {
- get: function() {
- return this._bgmOpen;
- },
- set: function(value) {
- this._bgmOpen = value;
- if (this._bgmOpen) {
- this.playBgm();
- } else {
- this.stopBgm();
- }
- cc.sys.localStorage.setItem("game_bgm_state", value);
- }
- },
- soundEffectOpen: {
- get: function() {
- return this._soundEffectOpen;
- },
- set: function(value) {
- this._soundEffectOpen = value;
- cc.sys.localStorage.setItem("game_sound_effect_state", value);
- }
- }
- },
- onLoad () {
- GameModule.audioMng = this;
- this.currentAudio = {};
- this.stopAll();
- let bgmState = cc.sys.localStorage.getItem("game_bgm_state");
- if (bgmState!= undefined) {
- if (typeof bgmState == "string" && bgmState == "") {
- this.bgmOpen = true;
- } else {
- this.bgmOpen = bgmState;
- }
- } else {
- this.bgmOpen = true;
- }
- let soundEffectState = cc.sys.localStorage.getItem("game_sound_effect_state");
- if (soundEffectState!= undefined) {
- if (typeof soundEffectState == "string" && soundEffectState == "") {
- this.soundEffectOpen = true;
- } else {
- this.soundEffectOpen = soundEffectState;
- }
- } else {
- this.soundEffectOpen = true;
- }
- this.playBgm();
- },
- /**
- * 暂停所有音效
- */
- stopAll () {
- cc.audioEngine.stopAll();
- delete this.currentAudio['bgm'];
- },
- /**
- * 播放背景音乐
- */
- playBgm () {
- if (this.currentAudio.hasOwnProperty('bgm')) {
- return;
- }
- if (!this.bgmOpen) { return }
- this.currentAudio['bgm'] = cc.audioEngine.play(this.bgm, true, this.volume);
- },
- /**
- * 停止播放背景音乐
- */
- pauseBgm () {
- cc.audioEngine.pause(this.currentAudio['bgm']);
- },
- /**
- * 暂停播放背景音乐
- */
- stopBgm () {
- cc.audioEngine.stop(this.currentAudio['bgm']);
- delete this.currentAudio['bgm'];
- },
- /**
- * 播放按钮音效
- */
- playClickButton () {
- if (!this.soundEffectOpen) { return }
- if (window.tt != undefined) {
- // if (this.playClickButtonAudioContext != null && this.playClickButtonAudioContext != undefined) {
- // // this.playClickButtonAudioContext.destory();
- // }
- this.playClickButtonAudioContext = tt.createInnerAudioContext();
- this.playClickButtonAudioContext.autoplay = false;
- this.playClickButtonAudioContext.src = 'https://pub.dwstatic.com/wxgame/taptapstar_toutiao_test/sheet/buttonClick.mp3';
- this.playClickButtonAudioContext.play();
- // this.playClickButtonAudioContext.onEnded(() => {
- // this.playClickButtonAudioContext.destory();
- // this.playClickButtonAudioContext = null;
- // })
- } else {
- cc.audioEngine.play(this.buttonClickAudio, false, this.volume);
- }
- },
- /**
- * 播放获得奖励音效
- */
- playGetAward () {
- if (!this.soundEffectOpen) { return }
- if (window.tt != undefined) {
- // if (this.playGetAwardAudioContext != null && this.playGetAwardAudioContext != undefined) {
- // // this.playGetAwardAudioContext.destory();
- // }
- this.playGetAwardAudioContext = tt.createInnerAudioContext();
- this.playGetAwardAudioContext.autoplay = false;
- this.playGetAwardAudioContext.src = 'https://pub.dwstatic.com/wxgame/taptapstar_toutiao_test/sheet/getAward.mp3';
- this.playGetAwardAudioContext.play();
- // this.playGetAwardAudioContext.onEnded(() => {
- // this.playGetAwardAudioContext.destory();
- // this.playGetAwardAudioContext = null;
- // })
- } else {
- cc.audioEngine.play(this.getAwardAudio, false, this.volume);
- }
- },
- /**
- * 播放升级建筑音效
- */
- playUpdateBuilding () {
- if (!this.soundEffectOpen) { return }
- if (window.tt != undefined) {
- // if (this.updateBuildingAudioContext != undefined && this.updateBuildingAudioContext != null) {
- // // this.updateBuildingAudioContext.destory();
- // }
- this.updateBuildingAudioContext = tt.createInnerAudioContext();
- this.updateBuildingAudioContext.autoplay = false;
- this.updateBuildingAudioContext.src = 'https://pub.dwstatic.com/wxgame/taptapstar_toutiao_test/sheet/updateBuilding.mp3';
- this.updateBuildingAudioContext.play();
- // this.updateBuildingAudioContext.onEnded(() => {
- // this.updateBuildingAudioContext.destory();
- // this.updateBuildingAudioContext = null;
- // })
- } else {
- cc.audioEngine.play(this.updateBuildingAudio, false, this.volume);
- }
- },
- /**
- * 点击招财猫
- */
- playClickCat () {
- if (!this.soundEffectOpen) { return }
- if (window.tt != undefined) {
- // if (this.innerAudioContext != undefined && this.innerAudioContext != null) {
- // // this.innerAudioContext.destory();
- // }
- this.innerAudioContext = tt.createInnerAudioContext();
- this.innerAudioContext.autoplay = false;
- this.innerAudioContext.src = 'https://pub.dwstatic.com/wxgame/taptapstar_toutiao_test/sheet/clickCat.mp3';
- this.innerAudioContext.play();
- // this.innerAudioContext.onEnded(() => {
- // this.innerAudioContext.destory();
- // this.innerAudioContext = null;
- // })
- } else {
- cc.audioEngine.play(this.clickCatAudio, false, this.volume)
- }
- },
- /**
- * 抽奖音效
- */
- playGetDraw () {
- if (!this.soundEffectOpen) { return }
- if (window.tt != undefined) {
- // if (this.getDrawAudioContext != undefined && this.getDrawAudioContext != null) {
- // // this.getDrawAudioContext.destory();
- // }
- this.getDrawAudioContext = tt.createInnerAudioContext();
- this.getDrawAudioContext.autoplay = false;
- this.getDrawAudioContext.src = 'https://pub.dwstatic.com/wxgame/taptapstar_toutiao_test/sheet/getDraw.mp3';
- this.getDrawAudioContext.play();
- // this.getDrawAudioContext.onEnded(() => {
- // this.getDrawAudioContext.destory();
- // this.getDrawAudioContext = null;
- // })
- } else {
- cc.audioEngine.play(this.getDrawAudio, false, this.volume);
- }
- },
- /**
- * 获取到明星音效
- */
- playGetStar () {
- if (!this.soundEffectOpen) { return }
- if (window.tt != undefined) {
- // if (this.playGetStarAudioContext != undefined && this.playGetStarAudioContext != null) {
- // // this.playGetStarAudioContext.destory();
- // }
- this.playGetStarAudioContext = tt.createInnerAudioContext();
- this.playGetStarAudioContext.autoplay = false;
- this.playGetStarAudioContext.src = 'https://pub.dwstatic.com/wxgame/taptapstar_toutiao_test/sheet/getStar.mp3';
- this.playGetStarAudioContext.play();
- // this.playGetStarAudioContext.onEnded(() => {
- // this.playGetStarAudioContext.destory();
- // this.playGetStarAudioContext = null;
- // })
- } else {
- cc.audioEngine.play(this.getStarAudio, false, this.volume);
- }
- },
- /**
- * 停止播放获取明星音效
- */
- // pauseGetStar () {
- // this.currentAudio['bgm'] = cc.audioEngine.play(this.bgm, true, this.volume);
- // cc.audioEngine.pause(this.currentAudio['bgm']);
- // },
- // update (dt) {},
- });
|