更改creator預製體的屬性

這裏以背景音樂爲例:

預製體music腳本

cc.Class({
    extends: cc.Component,

    properties: {
        bgAudio: {
            default: null,
            type: cc.AudioClip
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.bgAudio = cc.audioEngine.play(this.bgAudio, true, 1);
    },

    start() {

    },

    // update (dt) {},
});

調用:

cc.Class({
    extends: cc.Component,

    properties: {
        musicPrefab: {
            default: null,
            type: cc.Prefab
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        // 獲得預製體
        this.musicPrefab = cc.instantiate(this.musicPrefab);
        this.musicPrefab.parent = this.node;
        // 獲得預製體綁定的腳本
        this.musicPrefabScript = this.musicPrefab.getComponent('music');
    },

    start() {},

    onButtonCilck(event, customData) {
        switch (customData) {
            case "closeMusic":
                // 停止音樂
                cc.audioEngine.stop(this.musicPrefabScript.bgAudio);
                break;
            default:
                break
        }
    }
    // update (dt) {},


});

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章