GameAudio.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. if(index >= 0) {
  26. zy.playSound(this.brokens[index]);
  27. this._playPopEffect(count);
  28. }
  29. }, this);
  30. //过关
  31. cc.game.on(GameEvent.EventType.GAME_PASS_STAGE, () => {
  32. zy.playSound(this.wowClip);
  33. }, this);
  34. },
  35. _playPopEffect(count) {
  36. let index = 0;
  37. let playPopClip = () => {
  38. ++index;
  39. zy.playSound(this.popClip);
  40. if (index >= count) {
  41. this.unschedule(playPopClip);
  42. }
  43. }
  44. this.schedule(playPopClip, 0.08);
  45. },
  46. onDestroy() {
  47. //关闭所有音乐、音效
  48. cc.audioEngine.stopAll();
  49. //移除所有事件监听
  50. cc.game.targetOff(this);
  51. }
  52. });