forked from: flash on 2010-3-13

by hakerlab forked from flash on 2010-3-13 (diff: 1)
♥0 | Line 102 | Modified 2015-10-20 18:25:52 | MIT License
play

ActionScript3 source code

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

// forked from Mark's flash on 2010-3-13
package
{
    import flash.geom.Point;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    
    public class LinkMain extends MovieClip
    {
        private var text:TextField = new TextField();
        private var _numCircles:int = 75;
        private var _leapDist:int = 50;
        private var _moveSpeed:int = 1;
        private var _branchStrength:int = 500;
        private var _strengthMultiplier:int = 3;
        
        public function LinkMain ():void
        {
            for (var i:int = 0; i < _numCircles; i++)
            {
                var circle:MovieClip = new MovieClip();
                circle.name = "circle" + i;
                circle.leapDist = _leapDist;
                circle.numConnections = 0;
                circle.x = Math.random() * stage.stageWidth;
                circle.y = Math.random() * stage.stageHeight;
                circle.colour = Math.random() * 0xFFFFFF;
                circle.xVel = (Math.random() * _moveSpeed) - (_moveSpeed / 2);
                circle.yVel = (Math.random() * _moveSpeed) - (_moveSpeed / 2);
                circle.addEventListener(Event.ENTER_FRAME, onCircleLoop);
                stage.addChild(circle);
            }
            text.text = "Click screen to reverse attraction! Currently attracting";
            text.autoSize = "left";
            stage.addChild(text);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseHandle);        
        }
        private function onCircleLoop (evt:Event):void
        {
            var circle:MovieClip = MovieClip(evt.target);
            circle.x += circle.xVel;
            circle.y += circle.yVel;
            branchLines(circle);
            checkBounds(circle);
        }
        private function branchLines (circle:MovieClip):void
        {    
            circle.graphics.clear();
            circle.numConnections = 0;
            
            for (var i:int = 1; i < _numCircles; i++)
            {                
                if (Point.distance(new Point(stage.getChildAt(i).x, stage.getChildAt(i).y), new Point(circle.x, circle.y)) <= circle.leapDist)
                {
                    circle.graphics.moveTo(0, 0);
                    circle.graphics.lineStyle(3, circle.colour, 0.5);
                    circle.graphics.lineTo(stage.getChildAt(i).x - circle.x, stage.getChildAt(i).y - circle.y);
                    
                    circle.xVel += (stage.getChildAt(i).x - circle.x) / _branchStrength;
                    circle.yVel += (stage.getChildAt(i).y - circle.y) / _branchStrength;
                    MovieClip(stage.getChildAt(i)).xVel += (circle.x - stage.getChildAt(i).x) / _branchStrength;
                    MovieClip(stage.getChildAt(i)).yVel += (circle.y - stage.getChildAt(i).y) / _branchStrength;
                    
                    if(_branchStrength < 0)
                    {
                        circle.xVel *= 0.999;
                        circle.yVel *= 0.999;
                    }
                }
            }
            circle.graphics.moveTo(0, 0);
            circle.graphics.lineStyle(3, circle.colour);
            circle.graphics.drawCircle(0, 0, 3);    
        }
        private function mouseHandle (evt:Event):void
        {
            _branchStrength *= -1;    
            text.text = "Click screen to reverse attraction! ";
            if (_branchStrength >= 0) text.appendText("Currently attracting");
            else text.appendText("Currently repelling");
        }
        private function checkBounds (circle:MovieClip):void
        {
            if (circle.x <= 0)
            {
                circle.x = 0;
                circle.xVel *= -1;
            }
            if (circle.x >= stage.stageWidth)
            {
                circle.x = stage.stageWidth;
                circle.xVel *= -1;
            }
            if (circle.y <= 0)
            {
                circle.y = 0;
                circle.yVel *= -1;
            }
            if (circle.y >= stage.stageHeight)
            {
                circle.y = stage.stageHeight;
                circle.yVel *= -1;
            }
        }
    }
}