This is a recursive function which takes a DisplayObjectContainer (such as a Sprite or MovieClip) and rounds the x and y values of it and it's children. It's recursive as the function calls itself when it encounters a child which is also a DisplayObjectContainer so it's children also get rounded. This recurses the entire display list beneath the passed DisplayObjectContainer, it's children, it's children's children etc.
The point of this function is to make sure the passed DisplayObjectContainer and all it's children sit on whole pixels. This can be important when dealing with graphics which aren't sitting on whole pixels as it will cause them to appear blurry. This is probably most noticeable when dealing with pixel fonts.
function roundPositions(displayObjectContainer:DisplayObjectContainer):void { if (!(displayObjectContainer is Stage)) { displayObjectContainer.x = Math.round(displayObjectContainer.x); displayObjectContainer.y = Math.round(displayObjectContainer.y); } for (var i:uint = 0; i < displayObjectContainer.numChildren; i++) { var child:DisplayObject = displayObjectContainer.getChildAt(i); if (child is DisplayObjectContainer) { roundPositions(child as DisplayObjectContainer); } else { child.x = Math.round(child.x); child.y = Math.round(child.y); } } }
Comments
Subscribe to comments
You need to login to post a comment.

Much quicker than Math.round(): int( myObj.x + 0.5 );