Spring774
2025-11-12 2379cc78112ecbe65a9fcb817d6441875f785803
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import Userdata from "./Userdata";
 
const { ccclass, property } = cc._decorator;
const SOUND_BASE_PATH = 'audio/';
 
@ccclass
export default class SoundManager extends cc.Component {
    public static MIN_AUDIO_INTERVAL = 50;
 
    static lastPlayTimes = {};
    static shootId: number = -1;
    static loadAllAudioClips() {
        cc.loader.loadResDir(SOUND_BASE_PATH);
    }
 
    private static loadAudioClip(name: string, cb: (audioClip: cc.AudioClip) => void) {
        cc.loader.loadRes(SOUND_BASE_PATH + name, cc.AudioClip, (err, audioClip) => {
            if (err) {
                //cc.error('load audioclip error.', err);
                cb && cb(null);
                return;
            }
            cb && cb(audioClip);
        });
    }
 
    private static playEffect(name: string, cb?: (clipID: number) => void) {
        if (!Userdata.OpenAudio) {//未开启音效
            return;
        }
 
        let lastPlayTime = this.lastPlayTimes[name] || 0;
 
        if (Date.now() - lastPlayTime < this.MIN_AUDIO_INTERVAL) {
            return;
        }
        this.lastPlayTimes[name] = Date.now();
 
        this.loadAudioClip(name, (audioClip) => {
            if (audioClip) {
                let id = cc.audioEngine.playEffect(audioClip, false);
                cb && cb(id);
            }
        });
    }
 
    public static playButton() {
        this.playEffect('sound_btn');
    }
 
    static playStartAudio() {
        this.playEffect('sound_start');
    }
    static playWin() {
        this.playEffect('sound_win');
    }
 
    static playFailed() {
        this.playEffect('sound_failed');
    }
 
    static playBall() {
        this.playEffect('sound_ball');
    }
 
    static playProp() {
        this.playEffect('sound_prop');
    }
 
    static playStar(num: number) {
        this.playEffect('Star_' + num);
    }
}