Posted By

Activetuts on 01/13/11


Tagged


Versions (?)

Who likes this?

14 people have marked this snippet as a favorite

chus
PLyczkowski
elbjoern
EderChrono
toxin20
leadingmannigel
liakwee
kure69
AndrewPotter
shuchii
im3der
elderhsouza
coutlasssupreme
flashdeveloper


Round the Positional Values of a DisplayObjectContainer and its Children


 / Published in: ActionScript 3
 

URL: http://enva.to/e4ig6z

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.

  1. function roundPositions(displayObjectContainer:DisplayObjectContainer):void
  2. {
  3. if (!(displayObjectContainer is Stage)) {
  4. displayObjectContainer.x = Math.round(displayObjectContainer.x);
  5. displayObjectContainer.y = Math.round(displayObjectContainer.y);
  6. }
  7. for (var i:uint = 0; i < displayObjectContainer.numChildren; i++) {
  8. var child:DisplayObject = displayObjectContainer.getChildAt(i);
  9. if (child is DisplayObjectContainer) {
  10. roundPositions(child as DisplayObjectContainer);
  11. } else {
  12. child.x = Math.round(child.x);
  13. child.y = Math.round(child.y);
  14. }
  15. }
  16. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: divillysausages on January 2, 2012

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

You need to login to post a comment.