//我使用的TypeScript先用数组保存
labels: cc.Label[]=[];
buttons:cc.Button[]=[];
sprites:cc.Sprite[]=[];
audioSources:cc.AudioSource[]=[];
//在onLoad()中调用
this.labels=this.node.getComponentsInChildren(cc.Label);
this.sprites=this.node.getComponentsInChildren(cc.Sprite);
this.buttons=this.node.getComponentsInChildren(cc.Button);
this.audioSources=this.node.getComponentsInChildren(cc.AudioSource);
//然后写方法获取
GetButton(nodeName:string):cc.Button{
for (let index = 0; index < this.sprites.length; index++) {
if (this.sprites[index].name==nodeName) {
return this.buttons[index];
}
}
return null;
}
GetSprite(nodeName:string):cc.Sprite{
for (let index = 0; index < this.sprites.length; index++) {
if (this.sprites[index].name==nodeName) {
console.log("成功获取");
return this.sprites[index];
}
}
return null;
}
GetLabel(nodeName:string):cc.Label{
for (let index = 0; index < this.labels.length; index++) {
if (this.labels[index].name==nodeName) {
return this.labels[index];
}
}
return null;
}
//获取不到
// GetAudioSource(nodeName:string):cc.AudioSource{
// for (let index = 0; index < this.audioSources.length; index++) {
// if (this.audioSources[index].name==nodeName) {
// return this.audioSources[index];
// }
// }
// // this.audioSources.forEach(element => {
// // if (element.name==nodeName) {
// // return element;
// // }
// // });
// return null;
// }
this.GetSprite("white<Sprite>").node.active=true;
//这样注册了按钮的点击事件
this.buttons.forEach(element => {
let questionName=element.name.substring(0,3)
if (questionName=="Q1B"||questionName=="Q2A"||questionName=="Q3C") {
element.node.on("click",this.Correct,this);
} else {
element.node.on("click",this.Error,this);
}
this.SetNodeFadeOut0(element.node);
});
Correct(event){
//可以触发这个方法
//名字是对的
//但是输出是null;
console.log(this.GetButton("Q1B<Button>"));
}
//😖为什么?