获取prefab和获取脚本
properties: {
settingsPrefab: {
default: null,
type: cc.Prefab
},
}
this.settingsPrefab = cc.instantiate(this.settingsPrefab);
this.settingsPrefab.parent = this.node;
this.settingsPrefabScript = this.settingsPrefab.getComponent('setting');
替换label,图片
properties: {
nickname_label: cc.Label,
headimage: cc.Sprite,
gobal_count: cc.Label,
},
onLoad() {
this.nickname_label.string = myglobal.playerData.nickName;
this.gobal_count.string = ":" + myglobal.playerData.gobal_count;
var str = myglobal.playerData.avatarUrl;
var head_image_path = "UI/headimage/" + str;
cc.loader.loadRes(head_image_path, (err, img) => {
this.headimage.spriteFrame = new cc.SpriteFrame(img);
});
},
播放音乐和停止音乐
properties: {
bgAudio: {
default: null,
type: cc.AudioClip
}
},
this.bgAudioMusic = cc.audioEngine.stop(this.bgAudioMusic); //停止播放音乐
this.bgAudioMusic = cc.audioEngine.play(this.bgAudio, true, 1); //播放音乐
隐藏子节点
var gamebeforeUI = this.node.getChildByName("gamebeforeUI");
if (gamebeforeUI) {
gamebeforeUI.active = false;
}
本文介绍如何在Cocos Creator中使用脚本组件加载预制体(prefab)并实例化,同时演示了如何通过脚本更新UI元素,如Label和Sprite,以及如何控制背景音乐的播放和停止。此外,还提供了隐藏特定节点的方法。

7383

被折叠的 条评论
为什么被折叠?



