PlaySound.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. //背景音乐
  5. // Sound_BGM: {
  6. // default: null,
  7. // type: cc.AudioClip
  8. // },
  9. //点击普通按钮音效
  10. Sound_PressBtn: {
  11. default: null,
  12. type: cc.AudioClip
  13. },
  14. //赚到钱时增加收入时音效
  15. Sound_GetCoins: {
  16. default: null,
  17. type: cc.AudioClip
  18. },
  19. //店铺获得销售加成、农舍获取恢复体力加成时的音效
  20. Sound_BuildingAddition: {
  21. default: null,
  22. type: cc.AudioClip
  23. },
  24. //建造建筑时的音效
  25. Sound_FinishBuilding: {
  26. default: null,
  27. type: cc.AudioClip
  28. },
  29. //员工培训后升级的音效
  30. Sound_PeopleLevelUp: {
  31. default: null,
  32. type: cc.AudioClip
  33. },
  34. //招聘到员工时音效
  35. Sound_Recruit: {
  36. default: null,
  37. type: cc.AudioClip
  38. },
  39. OnOrOffMusicNode: {
  40. default: null,
  41. type: cc.Node,
  42. },
  43. OnOrOffSoundNode: {
  44. default: null,
  45. type: cc.Node,
  46. },
  47. OnImage: cc.SpriteFrame,
  48. OffImage: cc.SpriteFrame,
  49. IsMusicOn: true,
  50. IsSoundOn: true,
  51. // BGM: null,
  52. IsDebug: false,
  53. },
  54. // PlaySound:function (audioSource) {
  55. // audioSource.play();
  56. // },
  57. // onEnable(){
  58. // cc.log("cc.audioEngine.isMusicPlaying()",cc.audioEngine.isMusicPlaying())
  59. // },
  60. start() {
  61. // cc.game.on(cc.game.EVENT_SHOW, function () {
  62. // if (this.IsMusicOn&&this.BGM == 0) {
  63. // console.log("Play BGM!");
  64. // cc.audioEngine.resume(this.BGM);
  65. // }
  66. //
  67. // }.bind(this));
  68. this.IsMusicOn = true;
  69. this.IsSoundOn = true;
  70. if (!this.IsDebug) {
  71. // this.BGM = cc.audioEngine.play(this.Sound_BGM, true, 1);
  72. // cc.audioEngine.playMusic(this.Sound_BGM,1);
  73. this.stopAll();
  74. this.PlayBg();
  75. }
  76. UtilsWX.soundManage = this;
  77. },
  78. PlayBg : function(){
  79. if (this.BGM != null) {
  80. cc.audioEngine.stop(this.BGM);
  81. }
  82. cc.loader.loadRes('Sound/Sound_BGM', cc.AudioClip, function(err,clip){
  83. this.mclip = clip;
  84. this.BGM = cc.audioEngine.play(clip,false,1);
  85. }.bind(this));
  86. this.findWorkCallback = function () {
  87. // console.log("进来了吗");
  88. // this.BGM = cc.audioEngine.playMusic(this.mclip, false);
  89. if (this.BGM != null) {
  90. cc.audioEngine.stop(this.BGM);
  91. }
  92. cc.loader.loadRes('Sound/Sound_BGM', cc.AudioClip, function(err,clip){
  93. this.mclip = clip;
  94. this.BGM = cc.audioEngine.play(clip,false,1);
  95. // console.log("再次播放音乐");
  96. }.bind(this));
  97. }.bind(this);
  98. this.schedule(this.findWorkCallback, 33.4);
  99. },
  100. stopBg : function(){
  101. this.unschedule(this.findWorkCallback);
  102. cc.audioEngine.stop(this.BGM);
  103. },
  104. stopAll : function(){
  105. this.unschedule(this.findWorkCallback);
  106. cc.audioEngine.stopAll();
  107. },
  108. PlayPressBtn: function () {
  109. this.PlaySoundEffect(this.Sound_PressBtn, false, 1);
  110. },
  111. PlayGetCoins: function () {
  112. this.PlaySoundEffect(this.Sound_GetCoins, false, 1);
  113. },
  114. PlayBuildingAddition: function () {
  115. this.PlaySoundEffect(this.Sound_BuildingAddition, false, 1);
  116. },
  117. PlayFinishBuilding: function () {
  118. this.PlaySoundEffect(this.Sound_FinishBuilding, false, 1);
  119. },
  120. PlayPeopleLevelUp: function () {
  121. this.PlaySoundEffect(this.Sound_PeopleLevelUp, false, 1);
  122. },
  123. PlayRecruit: function () {
  124. this.PlaySoundEffect(this.Sound_Recruit, false, 1);
  125. },
  126. PlaySoundEffect: function (SoundEffect, IsLoop, Volume) {
  127. if (this.IsSoundOn) {
  128. cc.audioEngine.play(SoundEffect, IsLoop, Volume);
  129. }
  130. },
  131. PauseOrResumeSoundEffect: function () {
  132. if (this.IsSoundOn) {
  133. this.IsSoundOn = false;
  134. this.OnOrOffSoundNode.getComponent(cc.Sprite).spriteFrame = this.OffImage;
  135. } else {
  136. this.IsSoundOn = true;
  137. this.OnOrOffSoundNode.getComponent(cc.Sprite).spriteFrame = this.OnImage;
  138. }
  139. },
  140. PauseOrResumeMusic: function () {
  141. if (this.IsMusicOn) {
  142. this.IsMusicOn = false;
  143. // cc.audioEngine.pause(this.BGM);
  144. this.stopBg();
  145. this.OnOrOffMusicNode.getComponent(cc.Sprite).spriteFrame = this.OffImage;
  146. }
  147. else {
  148. this.IsMusicOn = true;
  149. // cc.audioEngine.resume(this.BGM);
  150. this.PlayBg();
  151. this.OnOrOffMusicNode.getComponent(cc.Sprite).spriteFrame = this.OnImage;
  152. }
  153. },
  154. startVideo: function () {
  155. if (this.IsMusicOn) {
  156. // cc.audioEngine.pause(this.BGM);
  157. this.PlayBg();
  158. // this.OnOrOffMusicNode.getComponent(cc.Sprite).spriteFrame = this.OffImage;
  159. }
  160. },
  161. stopVideo: function () {
  162. if (this.IsMusicOn) {
  163. // cc.audioEngine.resume(this.BGM);
  164. this.stopBg();
  165. // this.OnOrOffMusicNode.getComponent(cc.Sprite).spriteFrame = this.OnImage;
  166. }
  167. },
  168. // onLoad: function () {
  169. // this.current = cc.audioEngine.play(this.Sound_PressBtn, false, 1);
  170. // },
  171. //
  172. // onDestroy: function () {
  173. // cc.audioEngine.stop(this.current);
  174. // }
  175. });