In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

2D Z sorting

Get Adobe Flash player
by shapevent 14 Nov 2014
/**
 * Copyright shapevent ( http://wonderfl.net/user/shapevent )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/f54p
 */

package {
    import flash.display.Sprite;
    import flash.events.*;
    
    public class FlashTest extends Sprite {
        
        private var boxes:Array;
        private var BOX_NUM:int = 20;
        public function FlashTest() {
            var box:Box;
            
            boxes = [];
            
            for(var i:int = 0; i < BOX_NUM; i++){
               box = new Box();
               boxes.push(box);
               addChild(box);   
            }
            addEventListener(Event.ENTER_FRAME, onLoop);
        }
        
        private function onLoop(e:Event):void{
            boxes.sortOn("y", Array.NUMERIC);    
            for(var i:int = 0; i < BOX_NUM; i++){
              boxes[i].scaleX = boxes[i].scaleY = boxes[i].y / stage.stageHeight + 0.5;
              addChild(boxes[i]);    
            }
        }
    }
}
    
    import flash.display.Sprite;
    import com.greensock.TweenLite;
    import flash.events.*;
    
    class Box extends Sprite {
       public function Box() {
           graphics.beginFill(0);
           graphics.lineStyle(3, 0xFF0000);
           graphics.drawRect(0,0,50,50);
           addEventListener(Event.ADDED_TO_STAGE, onAdded);
           
       }   
       
       private function onAdded(e:Event):void{
         x = Math.random() * stage.stageWidth;
         y = Math.random() * stage.stageHeight;
         animate();
       }
       private function animate():void{
            var rx:Number = Math.random() * stage.stageWidth,
                ry:Number = Math.random() * stage.stageHeight;
           TweenLite.to(this, 3, {x : rx, y : ry, onComplete : animate});
       }
       
    }