最近搜索

第十三讲 讲解音频 音效 音乐

浏览:565
管理员 2021-08-13 04:47


如何播放音乐

cc.Class({
    extends: cc.Component,
    properties: {
        prefab_plane:cc.Prefab,
        audio_music:cc.AudioClip,//音乐
    },
     onLoad () {
        this.add_prefab();
     },
     add_prefab(){
        var node = cc.instantiate(this.prefab_plane);
        node.parent = this.node;


        node.x= parseInt(Math.random()*400);
        node.y= parseInt(Math.random()*200);
     },
    start () {
    },
    onClick(sender,str){
        console.log("333"+str);
        //cc.sys.openURL("http://java456.com/")
        //this.add_prefab();
        if(str=="music"){
            console.log(str);
            cc.audioEngine.play(this.audio_music,true,1);//true是否循环,,,1表示音量,0至1  可以是0.3
        }
    }

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


image.png

把音乐拖到 audio_music上面。

纠错,上面代码定义 的audioClip可能有问题。 下面是正确的定义。

image.png


停止音乐

image.png


播放背景音乐(他会判断背景音乐只能存在一个)

play不会判断。建议使用这个播放背景音乐。


image.png


设置背景音乐  音量


image.png







索引

方法


联系站长

站长微信:xiaomao0055

站长QQ:14496453