import { _decorator, Component, Node, loader, AudioSource, AnimationClip, AudioClip } from 'cc'; const { ccclass, property } = _decorator; @ccclass('AudioManager') export class AudioManager { private static _instance : AudioManager; private static _audioSource?: AudioSource; static get instance(){ if(this._instance){ return this._instance; } this._instance = new AudioManager(); return this._instance; } init(audioSource:AudioSource){ AudioManager._audioSource = audioSource; } playMusic(loop: boolean) { const audioSource = AudioManager._audioSource!; audioSource.loop= loop; audioSource?.play(); } playSound(name: string) { const audioSource = AudioManager._audioSource; const path = `audio/sound/${name}`; loader.loadRes(path, AudioClip, (err: any, clip: AudioClip) => { if (err) { console.warn(err); return; } audioSource?.playOneShot(clip,3); }); } }
//音乐的名称 enum AudioSource { BACK_GROUND='background', CLICK = 'click', CRASH = "crash", GET_MONEY = "getMoney", IN_CAR = "inCar", NEW_ORDER = "newOrder", START = "start", STOP = "stop", TOOTING1 = "tooting1", TOOTING2 = "tooting2", WIN = "win" } @ccclass('Constants') export class Constants extends Component { public static EventName = EventName; public static CustomerState = CustomerState; public static AudioSource = AudioSource; }
@property(AudioSource) private _audioSource : AudioSource | null = null; public onLoad() { this.mapManager?.resetMap(); if(this.carManager&&this.mapManager){ this.carManager?.resetCars(this.mapManager?.currentPath); } const audioSource = this.getComponent(AudioSource); this._audioSource = audioSource; //game.addPersistRootNode(this.node); if(this._audioSource){ AudioManager.instance.init(this._audioSource); AudioManager.instance.playMusic(true); } }
AudioManager.instance.playSound(Constants.AudioSource.CLICK);
可以设置一些部分的音效,拿到金币,刹车,关门。到终点。
站长微信:xiaomao0055
站长QQ:14496453