新手开发指南里的例子自己写没有效果

第一天学,刚看到获取其它组件的例子
var label = this.getComponent(cc.Label);
var text = this.name + ‘started’;

// Change the text in Label Component
label.string = text;

this.node可以获取到,this.node.getComponent(cc.Label)就为null了
节点是用的ui节点里的button

这是新建的js脚本


网页的控制台

萌毙中……

节点不对,要获取Label组件的话,this.node应该是名为Label节点,脚本应该添加在Label节点上
如果一定要在startButton节点上附加脚本的话,应该这样:
var child = this.node.getChildByName(“Label”);
var label = child.getComponent(cc.Label);
var text = this.name + ‘started’;
label.string = text;

1赞

还以为是向下寻找呢,getComponent只能get本身啊:sweat_smile: