Tag Archives: sorting

2D Z-Sort

Actionscript:
  1. var shapes:Array = new Array();
  2.  
  3. for (var i:int = 0; i<300; i++){
  4.     var s:Shape = new Shape();
  5.     s.x = Math.random()* stage.stageWidth;
  6.     s.y = Math.random()* stage.stageHeight;
  7.     s.graphics.lineStyle(0,0x000000);
  8.     s.graphics.beginFill(0xCCCCCC);
  9.     s.graphics.drawCircle(0,0, Math.random()*s.y / 5);
  10.     shapes.push(s);
  11. }
  12.  
  13. // comment this out to remove sorting
  14. shapes.sortOn("width", Array.NUMERIC);
  15.  
  16. for (i = 0; i<shapes.length; i++){
  17.     addChild(shapes[i]);
  18. }

This uses Array.sortOn() to do some basic z-sorting based on the width property of some circle shapes.

Posted in arrays, sortOn | Also tagged | Leave a comment