GameAudio.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * Player场景声音控制
  3. */
  4. // let GameEvent = require('GameEvent');
  5. let GameEvent = require('GameEvent');
  6. cc.Class({
  7. extends: cc.Component,
  8. editor: {
  9. menu: 'GameAudio(游戏声音脚本)',
  10. },
  11. properties: {
  12. bgmClip: { type: cc.AudioClip, default: null }, //背景音乐
  13. wowClip: { type: cc.AudioClip, default: null }, //过关音效
  14. startClip: { type: cc.AudioClip, default: null }, //开始音效
  15. selectClip: { type: cc.AudioClip, default: null }, //选中音效
  16. brokens: { type: cc.AudioClip, default: [] }, //消除音效
  17. popClip: { type: cc.AudioClip, default: null }
  18. },
  19. start () {
  20. zy.playBgMusic(this.bgmClip,true)
  21. zy.playSound(this.startClip);
  22. //矩阵事件监听
  23. cc.game.on(GameEvent.EventType.GAME_CLEAR_COUNT, (count) => {
  24. let index = Math.min(count - 1, this.brokens.length - 1);
  25. zy.playSound(this.brokens[index]);
  26. this._playPopEffect(count);
  27. }, this);
  28. //过关
  29. cc.game.on(GameEvent.EventType.GAME_PASS_STAGE, () => {
  30. zy.playSound(this.wowClip);
  31. }, this);
  32. },
  33. _playPopEffect(count) {
  34. let index = 0;
  35. let playPopClip = () => {
  36. ++index;
  37. zy.playSound(this.popClip);
  38. if (index >= count) {
  39. this.unschedule(playPopClip);
  40. }
  41. }
  42. this.schedule(playPopClip, 0.08);
  43. },
  44. onDestroy() {
  45. //关闭所有音乐、音效
  46. cc.audioEngine.stopAll();
  47. //移除所有事件监听
  48. cc.game.targetOff(this);
  49. }
  50. });