

cc.Class({
    extends: cc.Component,

    properties: {
        tipsAnim: cc.Animation,
        handAnim: cc.Animation,
    },

    data: null,

    onLoad() {
        this.tipsAnim.node.active = false
        this.handAnim.node.active = false
    },

    onEnable() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this)
    },

    onDisable() {
        this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this)
    },

    initWithData(data) {
        this.data = data
        let rect = this.data.rect
        this.node.x = rect.x
        this.node.y = rect.y
        this.node.width = rect.width
        this.node.height = rect.height
        if (this.data.useHand) {
            this.handAnim.node.active = false
            this.handAnim.play()
        }
    },

    onTouchStart() {
        if (this.data) {
            let url = this.data.audio_url
            if (url && url != '') {
                this.playAudio(url)
            }
        }
    },

    /** 播放音乐 */
    playAudio(url) {
        cc.assetManager.loadRemote(url, (err, audioClip) => {
            if (err) return
            this.audioId = cc.audioEngine.play(audioClip, false, 0.8);
            cc.audioEngine.setFinishCallback(this.audioId, () => {
                this.audioId = null
            });
        });
    },

    stopAudio() {
        if (this.audioId != null) {
            cc.audioEngine.stop(this.audioId)
            this.audioId = null
        }
    },
    /** 音频id */
    audioId: null,
});
