这里以背景音乐为例:
预制体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) {},
});