button如何在代码中设置title text?

试了很多种方法, 试图获得button的label组件, 再设置string属性, 但是获取的label组件都是cc.Node类型, 无法设置string属性.

this.node.getChildByName("Label").getComponent(cc.Label).string=123;
Label不是组件,是Button的Child

2赞

原来如此,多谢。这块明白点了

另外,我看属性面板,label也是button的组件,为什么不能用this.node.getComponent(cc.Label).string=“xxx”;来设置?

嗯…为什么会有label组件…

嗯,Label 组件的确不在 Button 根节点上,而是在其子 Label 节点上。

不过我觉得应该在 Button 组件中允许直接访问 Label,@zilong

我觉得没有任何必要,除非button和label有什么特殊的关联,目前看来也不是什么特殊的关联,不像scrollView与scrollViewBar的关系。。