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

forked from: CircleFitting

/**
 * Copyright alexnotkin ( http://wonderfl.net/user/alexnotkin )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/zUKr
 */

// forked from shapevent's CircleFitting
package {

    import flash.display.*;
    import flash.events.*;


    

       public class CircleFitting extends MovieClip {
        private var circs:Array;
        private var circNum:int;


               public function CircleFitting(){
                   
                   var centerX:Number = stage.stageWidth / 2;
                   var centerY:Number = stage.stageHeight / 2;
                   var back:Sprite = new Sprite();
      back.graphics.beginFill(0x000000);
      back.graphics.drawRect(0, 0, 2*centerX, 2*centerY);
      back.graphics.endFill();
      addChild (back);

                  // init
            circs = []
            circNum = 900;
            addEventListener(Event.ENTER_FRAME, onAdd);

               }
               // private methods

        private function onAdd(evt:Event):void {
            if (circs.length < circNum){
                makeGrowable();
            }
        }
        private function makeGrowable():void{
            
            var s:MovieClip = MovieClip(addChild(new MovieClip()));
            s.x = Math.random() * stage.stageWidth;
            s.y = Math.random() * stage.stageHeight;
            with(s.graphics){
                lineStyle(6,0xFFFFFF);
                drawCircle(6,0,15);
            }
            s.scaleX = s.scaleY = 0;
            circs.push(s);
            s.addEventListener(Event.ENTER_FRAME, onScaleUp);
        }
        private function onScaleUp(evt:Event):void {
            var c:MovieClip = MovieClip(evt.currentTarget);
            c.scaleX = c.scaleY += 0.05;
            for (var i:int = 0; i<circs.length; i++){
                var circ:MovieClip = circs[i];
                if (circ != c){
                    var amt:Number = circ.width/2 + c.width/2;
                    var dx:Number = circ.x - c.x;
                    var dy:Number = circ.y - c.y;
                    var dist:Number = Math.sqrt(dx * dx + dy * dy);
                    if (amt > dist){
                        c.removeEventListener(Event.ENTER_FRAME, onScaleUp);
                        if (c.scaleX < 0.1){
                            if (contains(c)){
                            removeChild(c);
                            }
                        }
                    }
                }
                
            }
        }
        

       }

}