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: flash Line

Get Adobe Flash player
by abiyasa 10 Jan 2011
// forked from hikechen's flash Line
package {
    import flash.geom.ColorTransform;
    import flash.utils.IDataInput;
    
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;

    public class Lines3D1 extends Sprite
    {
        private var balls:Array;
        private var ballsLine:Array;
        private var numBalls:uint = 50;
        private var fl:Number = 250;
        private var vpX:Number = stage.stageWidth / 2;
        private var vpY:Number = stage.stageHeight / 2;
        
        public function Lines3D1()
        {
            init();
        }
        
        private function init():void
        {
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            balls = new Array();
            ballsLine = new Array();
            for(var i:uint = 0; i < numBalls; i++)
            {
                var ball:Ball3D = new Ball3D(5, generatePastelColor());
                balls.push(ball);
                ballsLine.push(ball);
                ball.xpos = Math.random() * 200 - 100;
                ball.ypos = Math.random() * 200 - 100;
                ball.zpos = Math.random() * 200 - 100;
                addChild(ball);
            }
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        private function onEnterFrame(event:Event):void
        {
            var angleX:Number = (mouseY - vpY) * .001;
            var angleY:Number = (mouseX - vpX) * .001;
            for(var i:uint = 0; i < numBalls; i++)
            {
                var ball:Ball3D = balls[i];
                rotateX(ball, angleX);
                rotateY(ball, angleY);
                doPerspective(ball);
            }
            
            // do z-sorting
            sortZ();
            
            graphics.clear();
            graphics.lineStyle(0, 0xAAAAAA);    
            graphics.moveTo(balls[0].x, balls[0].y);
            for(i = 1; i < numBalls; i++)
            {
                graphics.lineTo(ballsLine[i].x, ballsLine[i].y);
            }
        }
        
        // generates pastel color 
        private function generatePastelColor():int
        {
            var red:int = ((Math.random() * 255) + 0x80) % 255;
            var green:int = ((Math.random() * 255) + 0x80) % 255;
            var blue:int = ((Math.random() * 255) + 0x80) % 255;
            
            return ((red << 16) | (green << 8) | blue);
        }
        
        private function sortZ():void 
        {
                balls.sortOn("zpos", Array.DESCENDING | Array.NUMERIC);
                var theBall:Ball3D;
            for (var i:int = 0; i < numBalls; i++) 
            {
                theBall = balls[i];
                setChildIndex(theBall, i);
            }
        }
                    
        private function rotateX(ball:Ball3D, angleX:Number):void
        {
            var cosX:Number = Math.cos(angleX);
            var sinX:Number = Math.sin(angleX);
            
            var y1:Number = ball.ypos * cosX - ball.zpos * sinX;
            var z1:Number = ball.zpos * cosX + ball.ypos * sinX;
            
            ball.ypos = y1;
            ball.zpos = z1;
        }
        private function rotateY(ball:Ball3D, angleY:Number):void
        {
            var cosY:Number = Math.cos(angleY);
            var sinY:Number = Math.sin(angleY);
            
            var x1:Number = ball.xpos * cosY - ball.zpos * sinY;
            var z1:Number = ball.zpos * cosY + ball.xpos * sinY;
            
            ball.xpos = x1;
            ball.zpos = z1;
        }
        
        private function doPerspective(ball:Ball3D):void
        {    
            var posZ:int = ball.zpos;        
            if(posZ > -fl)
            {
                var scale:Number = fl / (fl + posZ);
                ball.scaleX = ball.scaleY = scale;
                ball.x = vpX + ball.xpos * scale;
                ball.y = vpY + ball.ypos * scale;
                ball.visible = true;
                
                // handle fog effect
                var fogShading:int = posZ * 1.5;
                if (fogShading < -80)
                {
                    fogShading = -80;
                }
                var theColorTransform:ColorTransform = ball.transform.colorTransform;
                theColorTransform.redOffset = fogShading;
                theColorTransform.greenOffset = fogShading;
                theColorTransform.blueOffset = fogShading;
                ball.transform.colorTransform = theColorTransform;
            }
            else
            {
                ball.visible = false;
            }
        }
    }
}
import flash.display.Sprite;
class Ball3D extends Sprite {
         
        public var radius:Number;
        private var color:uint;
        public var xpos:Number = 0;
        public var ypos:Number = 0;
        public var zpos:Number = 0;
        public var vx:Number = 0;
        public var vy:Number = 0;
        public var vz:Number = 0;
        public var mass:Number = 1;
        
        public function Ball3D(radius:Number=40, color:int=0xff0000) {
            this.radius = radius;
            this.color = color;
            init();
        }
        public function init():void {
            graphics.lineStyle(0);
            graphics.beginFill(color);
            graphics.drawCircle(0, 0, radius);
            graphics.endFill();
        }
}