我用以下方式添加Label组件后无法获取到节点的内容大小,难道我要通过计算多少个字符的方式确定大小?
for (var i = 0; i < data.tag.length; i++) {
var nd = new cc.Node();
var lb = nd.addComponent(cc.Label);
lb.fontSize = 24;
lb.string = data.tag[i];
this.p2.addChild(nd);
if (i > 0) {
x2 += nd.getContentSize().width / 2;
}
nd.setPosition(x2, y2);
x2 = nd.getContentSize().width / 2 + 20;
if (x2 > 800) {
x2 = 0;
y2 = y2 - 26;
}
}
结果:
nd.getContentSize()
Size {width: 0, height: 0}height: 0width: 0__proto__: ValueType
nd.width
0