问题
Using Flash CS5 I came across something odd today.
The UIScrollBar component has the rigid width of 15px. When you add it to a container such as a Sprite, you'd expect the width of the sprite to return 15, but it returns 100 instead!
Here's an example code.
import flash.display.Sprite;
import fl.controls.UIScrollBar;
var spr:Sprite = new Sprite();
addChild(spr);
trace('spr.width:',spr.width);
var bar:UIScrollBar = new UIScrollBar();
spr.addChild(bar);
trace('bar.width',bar.width);
trace('spr.width:',spr.width);
Interestingly, output is
spr.width: 0
bar.width 15
spr.width: 100
Does anybody know what's happening there? Is this a bug?
回答1:
Found out that it's kind of a bug. What happens is, it takes some time for it to draw/render the component on the stage so its size becomes accessible only after it's drawn. For the record, in my computer it takes 1 ms. until it returns the expected value.
Another surprising issue is, UIScrollBar returns 15px as its width whereas the container sprite returns 16px. For those looking for the reason, it's probably related to the outline of the bar, which is drawn with thickness=1
and scaleMode=LineScaleMode.NONE
. I had the exact same issue with a class I wrote, in which I had to override the width and height getters by adding 1 to the return values of each super method.
来源:https://stackoverflow.com/questions/11677547/actionscript-3-uiscrollbar-bug-width-of-container-returns-85px