标签:value effect prope url 初始 ade this bsp fun
首先一个配音id集合的json文件,AudioConfig.json:
1 [ 2 {"id":1,"path":"bgm/menuBgm"}, 3 {"id":2,"path":"bgm/BGM_001"}, 4 {"id":3,"path":"bgm/BGM_002"}, 5 {"id":4,"path":"bgm/BGM_003"}, 6 {"id":5,"path":"bgm/BGM_004"}, 7 {"id":6,"path":"bgm/BGM_005"}, 8 {"id":7,"path":"bgm/BGM_006"}, 9 {"id":8,"path":"bgm/BGM_007"}, 10 {"id":9,"path":"bgm/Annie_hello"}, 11 {"id":10,"path":"bgm/Timmy_Hi_Annie_How_are_you_today"} 12 ]
json文件加载处理,配音调用类PlaySound类:
1 cc.Class({ 2 extends: cc.Component, 3 4 properties: { 5 _musicVolume: 1.0, 6 _playEffect: [], 7 _switchEffect: true 8 }, 9 10 onLoad () { 11 cc.loader.load(cc.url.raw(‘resources/AudioConfig.json‘), (err,object) => { 12 13 if (err) { 14 cc.log(err); 15 } 16 else{ 17 cc.log("我有数据啦"); 18 var list = object; 19 20 var nlength = list.length; 21 // cc.log("nlength = " + nlength); 22 this.Arr = new Array(nlength); 23 for (let i=0;i<nlength;i++) 24 { 25 var arrvalue = list[i]; 26 // 把数组当字典使用 27 this.Arr[arrvalue.id] = arrvalue.path; 28 } 29 30 31 } 32 }); 33 }, 34 35 36 start () { 37 38 }, 39 40 PlaySoundId: function(id) { 41 var _url = "audio/" + this.Arr[id]; 42 // cc.log("PlaySoundId将要获取的id对应路径:" + str); 43 // loadRes 不要.mp3后缀名,如果有后缀名则警告 44 cc.loader.loadRes(_url, (err, asset)=> { 45 this.playEffect(_url); 46 }); 47 }, 48 49 playEffect: function(url){ 50 if(this._switchEffect){ 51 var rawUrl = cc.url.raw("resources/" + url); 52 cc.log("resources/"+url); 53 54 // getRes 要.mp3后缀名,否则要报警告 55 if(cc.loader.getRes(rawUrl+".mp3")){ 56 var effectId = cc.audioEngine.playEffect(rawUrl+".mp3", false, this._musicVolume); 57 this._playEffect[url] = effectId; 58 cc.log("声音播放"); 59 } 60 else{ 61 cc.log("【音频】音效" + url + "文件不存在"); 62 } 63 } 64 65 66 }, 67 // update (dt) {}, 68 });
配音文件在初始化时,既已调用,要把脚本添加到要添加的节点上,推荐添加到根节点,通过这个节点,进行一次加载即可在其他需要播放声音的时候,直接调用即可。
标签:value effect prope url 初始 ade this bsp fun
原文地址:https://www.cnblogs.com/Hunter-541695/p/9880099.html