Useful Flex Tip: Use CallLater()
Have you ever tried to do something like this:
var someText:Text = new Text();
someText.text="testing";
addChild(someText);
trace(someText.measuredWidth);
and then scratched your head when Flex traces 0 for the width? That's because Flex hasn't drawn it yet. There's two ways to fix it, one you could just call someText.validateSize() but the better way to do it is to wait till Flex validates on its own to make these changes. How do you do that? Well with the callLater() function of course!
var someText:Text = new Text();
someText.text="testing";
addChild(someText);
callLater(getWidth);
public function getWidth():void{
trace(someText.measuredWidth);
}
ahhh... there we go. Now Flex should be tracing the correct width. This works really well for a number of instances where you want to play with freshly declared visual elements but Flex hasn't finished creating them yet.

0 Comments:
Post a Comment
<< Home