running this code
var sp:Sprite = new Sprite();
sp.graphics.beginFill(0xff);
sp.graphics.drawRect(0,0,400,400);
sp.graphics.endFill();
sp.scrollRect = new Rectangle(0,0,350,350);
addChild(sp);
var count:int = 0;
var f:Function = function(...args):void{
trace(count++,sp.width,sp.height);
if(count>5){
removeEventListener(Event.ENTER_FRAME,f);
}
}
addEventListener(Event.ENTER_FRAME,f);
get
0 400 400
1 350 350
2 350 350
3 350 350
4 350 350
5 350 350
is there anyway i can made the scrollRect work at the first frame?
I just checked, and its probably because of some internal rendering stuff in Flash Player (it actually takes two frames to get the real dimensions, the current one, and the one after). Visually the rectangle doesn’t change sizes, so it’s only the values of the object’s bounds that are updated two frames later (you can easily prove this by setting a very low framerate, like 1 FPS).
One workaround is to force a “hard” rendering pass by drawing the stage to a 1×1 BitmapData… this will effectively update the entire display list, and those values should be correct immediately after the draw() call. So your code would look like this:
(Note that I changed the way you declared the function, as this is the preferred way among most (if not all) Flash developers.)
You could do the draw call only once after adding the object to the stage, but I don’t know how safe it would be if the object changes.