SoundEffectButton.ts 1.58 KB
// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import { GameBasicSettings } from "../gameplay/settings/GameBasicSettings";

const { ccclass, property } = cc._decorator;

@ccclass

export default class SoundEffectButton extends cc.Component {

    @property({ type: cc.AudioClip })
    clip: cc.AudioClip = null as any as cc.AudioClip;

    interactable: boolean = true;

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
    }

    onEnable() {
        this.node.on(cc.Node.EventType.TOUCH_END, this.onPlaySoundEffectCallback, this);
    }

    onDisable() {
        this.node.off(cc.Node.EventType.TOUCH_END, this.onPlaySoundEffectCallback, this);
    }

    onPlaySoundEffectCallback(event) {
        if (!this.clip) {
            console.log("current clip is null.");
        } else if (this.interactable) {
            if (GameBasicSettings.soundVolume != 0)
                cc.audioEngine.playEffect(this.clip, false)
        }
    }

    start() {

    }

    // update (dt) {}
}