Making Connections

by bradsedito
♥2 | Line 111 | Modified 2013-10-26 00:37:17 | MIT License
play

ActionScript3 source code

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





package
{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.filters.BlurFilter;
    import flash.filters.GlowFilter;
    import net.hires.debug.Stats;


    public class MakingConnections extends Sprite 
    {
        private       const NUM_PARTICLE :Number        =  20;
        private       var   blurr         :BlurFilter   =  new BlurFilter( 4,4,2 );
        public static var   h             :int;
        public static var   w             :int;        
        private       var   balls         :Vector.<Ball>;
        private       var   cballs        :Vector.<Ball>;
        private       var   l             :Graphics;
        private       var   lines         :Sprite; 
        private       var   stats         :Stats = new Stats();
        
        
        public function MakingConnections()
        {
            stage.frameRate = 90
            w = stage.stageWidth
            h = stage.stageHeight
            stage.addChild( stats );
            //ENTRY POINT:
            
            this.graphics.beginFill( 0 )
            this.graphics.drawRect( 0,0,w,h )
            
            lines  =  new Sprite()
            
            var gfltr:GlowFilter  =  new GlowFilter( 0xFFFFFF )  //0x0055ff )
            gfltr.blurX = gfltr.blurY = 3
            lines.filters=[ gfltr,blurr ]
            addChild( lines )
            
            lines.blendMode = BlendMode.LIGHTEN
           
            l      =  lines.graphics
            balls  =  new Vector.<Ball>()
            cballs =  new Vector.<Ball>()
            
            for (var i : int = 0; i < NUM_PARTICLE; i++) 
            {
                var b:Ball=new Ball(0xffffff)
                b.x   =  Math.random()*w
                b.y   =  Math.random()*h
                b.h   =  h
                b.w   =  w
                b.vx  =  Math.random()*2//5
                b.vy  =  Math.random()*2
                b.g   =  Math.random()/2//5
                               
                balls.    push( b )
                cballs.   push( b )
                stage.addChild( b )
            }
            addEventListener(Event.ENTER_FRAME, loop)
        }


        private function loop(event : Event) : void 
        {
            l.clear()
            l.lineStyle( 0.6,0x0066ff )
            //l.lineStyle(.1,0x0000ff)

            for (var i : int = 0; i < NUM_PARTICLE; i++) 
            {
                var bi:Ball=balls[i]
                bi.render()
                bi.hited=true     

                for (var j:int=0; j<NUM_PARTICLE; j++) 
                {
                    var bj:Ball  =  balls[ j ]
                    var dist:Number=Point.distance(new Point(bi.x,bi.y),new Point(bj.x,bj.y))
                    
                    bj.alpha = 0.100;
                    
                    if( bj.hited  ==  false  &&  dist<100 )
                    {
                        l.moveTo(bi.x, bi.y)
                        l.lineTo(bj.x, bj.y)
                    }
                    balls[j].hited=false
                }
            }
        }
    }

}



import flash.display.Shape;
class Ball extends Shape
{
    public var w     :int
    public var h     :int
    public var vx    :Number
    public var vy    :Number
    public var g     :Number   =  0.001
    public var hited :Boolean  =  false

    public function Ball(  c:uint=0, rad:uint=10, _vx:Number=1, _vy:Number=1, _g:Number=0.001 ):void    //public function Ball(c:uint=0,rad:uint=5,_vx:Number=2,_vy:Number=2,_g:Number=.1):void
    {
        this.vy  =  _vy;
        this.vx  =  _vx;
        this.g   =  _g;
        
        this.graphics.beginFill( c )
        this.graphics.drawCircle( 0,0,rad )
        this.graphics.endFill()
    }

    public function render():void
    {
        this.vx  +=  0
        this.vy  +=  0
        this.x   +=  vx
        this.y   +=  vy

        if(  this.y > h  )  /*THEN..*/  this.vy  *=  -1
        if(  this.y < 0  )  /*THEN..*/  this.vy  *=  -1
        if(  this.x > w  )  /*THEN..*/  this.vx  *=  -1
        if(  this.x < 0  )  /*THEN..*/  this.vx  *=  -1
    }
}