{"id":728,"date":"2020-09-05T16:48:03","date_gmt":"2020-09-05T08:48:03","guid":{"rendered":"http:\/\/blog.xrhoujie.com\/?p=728"},"modified":"2020-09-02T16:51:01","modified_gmt":"2020-09-02T08:51:01","slug":"egret%e6%88%90%e8%af%ad%e5%a4%a7%e6%8c%91%e6%88%98-%e9%9f%b3%e9%a2%91%e5%aa%92%e4%bd%93%e7%ae%a1%e7%90%86%e4%ba%8c","status":"publish","type":"post","link":"http:\/\/blog.xrhoujie.com\/?p=728","title":{"rendered":"Egret\u6210\u8bed\u5927\u6311\u6218-\u97f3\u9891\u5a92\u4f53\u7ba1\u7406(\u4e8c)"},"content":{"rendered":"<p><code><br \/>\nclass SoundManage {<br \/>\n\tprivate bg_sound: egret.Sound;<br \/>\n\tprivate tapWord_sound: egret.Sound;<br \/>\n\tprivate click_sound: egret.Sound;<br \/>\n\tprivate right_sound: egret.Sound;<br \/>\n\tprivate wrong_sound: egret.Sound;<\/p>\n<p>\tprivate soundChannel: egret.SoundChannel;<\/p>\n<p>\tpublic static shared:SoundManage;<br \/>\n\tpublic static getInstance(){<br \/>\n\t\tif(!SoundManage.shared){<br \/>\n\t\t\tSoundManage.shared = new SoundManage;<br \/>\n\t\t}else{<br \/>\n\t\t\treturn SoundManage.shared;<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\tpublic constructor() {<br \/>\n\t\tthis.bg_sound = new egret.Sound();<br \/>\n\t\tthis.bg_sound.load('resource\/assets\/data\/sound\/Music.mp3');<br \/>\n\t\tthis.bg_sound.addEventListener(egret.Event.SOUND_COMPLETE,()=>{<br \/>\n\t\t\tthis.playBgMusic();<br \/>\n\t\t},this)<\/p>\n<p>\t\tthis.click_sound = new egret.Sound();<br \/>\n\t\tthis.click_sound.load('resource\/assets\/data\/sound\/buttonclick.mp3')<\/p>\n<p>\t\tthis.right_sound = new egret.Sound();<br \/>\n\t\tthis.right_sound.load('resource\/assets\/data\/sound\/right.mp3')<\/p>\n<p>\t\tthis.wrong_sound = new egret.Sound();<br \/>\n\t\tthis.wrong_sound.load('resource\/assets\/data\/sound\/wrong.mp3')<\/p>\n<p>\t\tthis.tapWord_sound = new egret.Sound();<br \/>\n\t\tthis.tapWord_sound.load('resource\/assets\/data\/sound\/type_word.mp3')<br \/>\n\t}<br \/>\n\tpublic playBgMusic() {<br \/>\n\t\tif(this.bg_sound && this.isMusic){<br \/>\n\t\t\tthis.soundChannel = this.bg_sound.play(0,0);<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\tpublic stopBgMusic() {<br \/>\n\t\tif(this.soundChannel){<br \/>\n\t\t\tthis.soundChannel.stop()<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\tpublic playClick() {<br \/>\n\t\tif(this.isEffect && this.click_sound){<br \/>\n\t\t\tthis.click_sound.play(0,1);<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\tpublic playTapWord() {<br \/>\n\t\tif(this.isEffect && this.tapWord_sound){<br \/>\n\t\t\tthis.tapWord_sound.play(0,1);<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\tpublic playRight() {<br \/>\n\t\tif(this.isEffect && this.right_sound){<br \/>\n\t\t\tthis.right_sound.play(0,1);<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\tpublic playWrong() {<br \/>\n\t\tif(this.isEffect && this.wrong_sound){<br \/>\n\t\t\tthis.wrong_sound.play(0,1);<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\t\/\/\u8bbe\u7f6e\u662f\u5426\u64ad\u653e\u80cc\u666f\u97f3\u9891<br \/>\n\tpublic set isMusic(val){<br \/>\n\t\tif(val){<br \/>\n\t\t\tegret.localStorage.setItem('isMusic','1');<br \/>\n\t\t\tthis.playBgMusic();<br \/>\n\t\t}else{<br \/>\n\t\t\tegret.localStorage.setItem('isMusic','0');<br \/>\n\t\t\tthis.stopBgMusic();<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\t\/\/\u83b7\u53d6\u80cc\u666f\u97f3\u9891\u662f\u5426\u64ad\u653e<br \/>\n\tpublic get isMusic(){<br \/>\n        let ret = egret.localStorage.getItem('isMusic');<br \/>\n\t\tif(ret == null || ret == ''){<br \/>\n\t\t\treturn true<br \/>\n\t\t}else{<br \/>\n\t\t\treturn ret == '1'<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\t\/\/\u8bbe\u7f6e\u662f\u5426\u64ad\u653e\u80cc\u666f\u97f3\u9891<br \/>\n\tpublic set isEffect(val){<br \/>\n\t\tif(val){<br \/>\n\t\t\tegret.localStorage.setItem('isSound','1');<br \/>\n\t\t}else{<br \/>\n\t\t\tegret.localStorage.setItem('isSound','0');<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\t\/\/\u83b7\u53d6\u80cc\u666f\u97f3\u9891\u662f\u5426\u64ad\u653e<br \/>\n\tpublic get isEffect(){<br \/>\n        let ret = egret.localStorage.getItem('isSound');<br \/>\n\t\tif(ret == null || ret == ''){<br \/>\n\t\t\treturn true<br \/>\n\t\t}else{<br \/>\n\t\t\treturn ret == '1'<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>class SoundManage { private bg_sound: egret.Sound; priv [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":469,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[15],"_links":{"self":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/728"}],"collection":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=728"}],"version-history":[{"count":1,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/728\/revisions"}],"predecessor-version":[{"id":729,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/728\/revisions\/729"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/media\/469"}],"wp:attachment":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=728"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}