Widget为什么在场景编辑中有效,代码中控制无效

	//绘制能量背景
	this.powerBarBg = cc.instantiate(this.customRoundRect);
	this.top.addChild(this.powerBarBg);
	this.powerBarBg.width=this.mid.width-GameConfig.tileGap*2;
	var customRoundRect=this.powerBarBg.getComponent("CustomRoundRect");
	customRoundRect.fillColor="#e7e7e7";
	var widget=this.powerBarBg.getComponent(cc.Widget);
	widget.bottom=10;
	widget.left=10;
	cc.log(widget)

所以log的结果是?

打印出来是变了,可是界面显示没有变

调用widget的方法,立即更新就好了,不然要下一帧才会更新

可能是widget的alignMode=ONCE导致的,在界面初始化时已经刷新了一次了。可以通过updateAlignment()方法手动刷新一次。

按理说靠下点,可实际不是这样

修改之后的代码:
//绘制能量背景
this.powerBarBg = cc.instantiate(this.customRoundRect);
this.top.addChild(this.powerBarBg);
this.powerBarBg.width=this.mid.width-GameConfig.tileGap*2;
var customRoundRect=this.powerBarBg.getComponent(“CustomRoundRect”);
customRoundRect.fillColor="#e7e7e7";
var widget=this.powerBarBg.getComponent(cc.Widget);
widget.bottom=10;
widget.left=10;
widget.updateAlignment();
cc.log(widget)

添加了widget.updateAlignment();还是一样

调用之后不会出现这个情况,请再检查一下代码,最好将在编辑器中的结果,数据和实际代码调用的结果,数据都贴出来,而不是简单的说“变了”或者“没变”。

对比看就知道了,代码都贴出来了…

这个问题我也复现了,不知道解决了没?