// Learn TypeScript:
// - Use TypeScript | Cocos Creator
// Learn Attribute:
// - Attribute reference | Cocos Creator
// Learn life-cycle callbacks:
// - Life cycle callback | Cocos Creator
const { ccclass, property } = cc._decorator;
@ccclass
export default class NewClass extends cc.Component {
@property(cc.Label)
mlabel: cc.Label = null;
@property(cc.Label)
elabel: cc.Label = null;
music = null;
effect = null;
// LIFE-CYCLE CALLBACKS:
async onLoad() {
await new Promise((resolve, reject) => {
cc.assetManager.loadBundle("c", (err, bundle) => {
let a = 0;
bundle.load("mainbgm", cc.AudioClip, (err, clip) => {
this.music = clip;
a++;
if (a == 2) {
resolve('ok');
}
});
bundle.load("click", cc.AudioClip, (err, clip) => {
this.effect = clip;
a++;
if (a == 2) {
resolve('ok');
}
});
});
});
this.node.getChildByName('music').on('click', () => {
if (this.mlabel.string == "音乐") {
cc.audioEngine.playMusic(this.music, true);
cc.audioEngine.setMusicVolume(1);
this.mlabel.string = '音乐开';
} else {
cc.audioEngine.stopMusic();
cc.audioEngine.setMusicVolume(0);
this.mlabel.string = '音乐';
}
});
this.node.getChildByName('effect').on('click', () => {
if (this.elabel.string == "音效") {
// console.log(this.effect, this.music);
cc.audioEngine.playEffect(this.effect, true);
cc.audioEngine.setEffectsVolume(1);
this.elabel.string = '音效开';
} else {
cc.audioEngine.stopAllEffects();
cc.audioEngine.setEffectsVolume(0);
this.elabel.string = '音效';
}
})
}
start() {
}
// update (dt) {}
}
源码如上
-
Creator 版本:
2.4.11 -
目标平台:
web mobile -
重现方式:
-
首个报错:
-
之前哪个版本是正常的:
-
手机型号:
-
手机浏览器:
-
编辑器操作系统:
-
重现概率: