Beautifl - Flash Gallery

Thumbnail : Node Garden Flash
Node Garden Flash
gaurav.rane 2013-01-15 see code comments

再生するにはFlash Playerが必要です。デスクトップのブラウザでご覧ください。

//From Node Garden
//http://jsdo.it/Evan/apyQ

//Bored, trying to replicate Assassins Creed III Menu background.
package

{
    import flash.filters.BlurFilter;
    import flash.display.MovieClip;
    import flash.display.GradientType;
    import flash.geom.Matrix;

    import flash.display.Graphics;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.geom.Point;
    import flash.utils.Timer;
    import net.hires.debug.Stats;

    [SWF(frameRate="60",width="465",height="465")]

    public class Connected extends Sprite
    {
        public var nodesarray:Array;

        public var timer:Timer;

        public var SCREEN_WIDTH:Number = stage.stageWidth;

        public var SCREEN_HEIGHT:Number = stage.stageHeight;

        

        public function Connected()
        {
            super();
           
            stage.scaleMode = "noScale";
            stage.align = "TL";
            stage.addChild(new Stats());
            
            createnodes();

            var s:Sprite = new Sprite;
            s.graphics.beginFill(0xcae3ed,.7);
            s.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
            s.graphics.endFill();
           
            addChild(s);
          
            timer = new Timer(30,0);
            timer.addEventListener(TimerEvent.TIMER,ontimer);
            timer.start();
        }

        private function createnodes():void
        {
            nodesarray = [];
            for(var i:int = 0; i < 50; i ++) 
            {
                var n:Node = new Node(stage.stageWidth,stage.stageHeight);
                nodesarray[nodesarray.length] = addChild(n);
            }
        }

        

        private function ontimer(e:TimerEvent):void
        {
            var g:Graphics = graphics;
            g.clear();

            var mtx:Matrix = new Matrix();
            mtx.createGradientBox(500*2,500*2,0,-230,-230);
            g.beginGradientFill(GradientType.RADIAL, [0xFFFFFF, 0xefefef], [1,1], [0,255], mtx);
            g.drawCircle(250,250,stage.stageWidth);

            for(var i:int = 0 ; i < nodesarray.length; i++)
            {
                var n:Node = nodesarray[i];
                n.update(stage.stageWidth,stage.stageHeight);
                n.draw();
            }
            for(var j:int = 0 ; j < nodesarray.length - 1; j++)
            {
                var node1:Node = nodesarray[j];
                for(var k:int = j + 1 ; k < nodesarray.length; k++)
                {
                    var node2:Node = nodesarray[k];
                    spring(node1,node2);
                }
            }
        }

        

        private function spring(na:Object,nb:Object):void
        {
            var dx:int = nb.x - na.x;
            var dy:int = nb.y - na.y;
            var dist:int = Math.sqrt(dx*dx + dy*dy);

            if (dist < 90) 
            {
                var g:Graphics = graphics;
                g.lineStyle(1,0xABC8E2,1 - (dist / 90));
                g.moveTo(na.x, na.y);
                g.lineTo(nb.x, nb.y);
                var ax:int = dx * 0.0001;
                var ay:int = dy * 0.0001;
                na.vx += ax;
                na.vy += ay;
                nb.vx -= ax;
                nb.vy -= ay;
            }
        }
    }
}



    import flash.display.Graphics;
    import flash.display.Shape;
    import flash.geom.Point;

    class Node extends Shape
    {
        public var point:Point = new Point();
        public var SCREEN_WIDTH:Number;
        public var SCREEN_HEIGHT:Number;
        public var vx:int;
        public var vy:int;

       public function Node(nwidth:Number,nheight:Number)
        {
            super();
            SCREEN_WIDTH = nwidth;
            SCREEN_HEIGHT = nheight;
            this.x = Math.random()* SCREEN_WIDTH;
            this.y = Math.random()* SCREEN_HEIGHT;
            this.vx = Math.random()*6-3;
            this.vy = Math.random()*6-3;
        }

        public function update(nwidth:Number,nheight:Number):void
        {
            SCREEN_WIDTH = nwidth;
            SCREEN_HEIGHT = nheight;
            this.x += this.vx;
            this.y += this.vy;
            if (this.x > SCREEN_WIDTH) 
            {
                this.x = 0;
            } 
            else if (this.x < 0) 
            {
                this.x = SCREEN_WIDTH;
            }
            if (this.y > SCREEN_HEIGHT) 
            {
                this.y = 0;
            } 
            else if (this.y < 0) 
            {
                this.y = SCREEN_HEIGHT;
            }
        }

        public function draw():void
        {
            var g:Graphics = graphics;
            g.clear();
            g.beginFill(0x375D81, 0.9);
            g.drawCircle(0, 0, 2);
            g.endFill();
        }
    }