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: Cosmos

Get Adobe Flash player
by 0xABCDEF 30 Jun 2011
/**
 * Copyright 0xABCDEF ( http://wonderfl.net/user/0xABCDEF )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/rEfj
 */

// forked from gameegg's Cosmos
package {
    import flash.ui.Keyboard;
    
    
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.KeyboardEvent;
    
    public class Cosmos extends Sprite {
        
        private var space:Sprite = new Sprite();
        private var lineSpace:Sprite = new Sprite();
        
        private var creating:Boolean;
        private var planetScale:Number;
        
        private var mouseSx:Number;
        private var mouseSy:Number;
        private var mouseDx:Number;
        private var mouseDy:Number;
        private var mouseDownBool:Boolean;
        private var enterBool:Boolean;
        private var spaceBool:Boolean;
        
        private var dx:Number=0;
        private var dy:Number=0;
        private var pvx:Number;
        private var pvy:Number;
        
        
        public function Cosmos() {
            creating = false;
            planetScale = 1;
            
            pvx=mouseX;
            pvy=mouseY;
            addChild(space);
            addChild(lineSpace);
            space.graphics.beginFill(0x111111);
            space.graphics.drawRect(0,0,465,465);
            lineSpace.graphics.lineStyle(1,0xaa0000);
            
            stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);
            stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMove);
            stage.addEventListener(MouseEvent.MOUSE_UP,mouseUp);
            stage.addEventListener(KeyboardEvent.KEY_UP,keyUp);
            stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
            
            addEventListener(Event.ENTER_FRAME,enterFrame);
        }
        
        private function enterFrame(e:Event):void{
            
            if( creating )
            {
                planetScale *= 1.3;
                with( lineSpace.graphics )
                {
                    clear();
                    lineStyle(2,0x880000);
                    moveTo(mouseSx,mouseSy);
                    lineTo(mouseX,mouseY);
                    lineStyle( -1 );
                    beginFill( 0xFFFFFF );
                    drawCircle( mouseSx, mouseSy, Math.pow( planetScale, 1/3 ) );
                    endFill();
                }
            }
            
            for(var i:int = 0; i < space.numChildren; ++i){
                
                for(var j:int = i+1; j < space.numChildren; ++j){
                    if(distance(Planet(space.getChildAt(i)),Planet(space.getChildAt(j))) > Planet(space.getChildAt(i)).r + Planet(space.getChildAt(j)).r){
                        gravity(Planet(space.getChildAt(i)),Planet(space.getChildAt(j)));
                    }
                    else{
                        fusionPlanet(Planet(space.getChildAt(i)),Planet(space.getChildAt(j)));
                    }

                }
                if(!Planet(space.getChildAt(i)).blackhole){
                    
                    Planet(space.getChildAt(i)).x += Planet(space.getChildAt(i)).pX/Planet(space.getChildAt(i)).m;
                    Planet(space.getChildAt(i)).y += Planet(space.getChildAt(i)).pY/Planet(space.getChildAt(i)).m;
                }
                else{
                    Planet(space.getChildAt(i)).x = mouseX;
                    Planet(space.getChildAt(i)).y = mouseY;
                }

               
                /*
                if(Planet(space.getChildAt(i)).x > 465){
                    Planet(space.getChildAt(i)).x = 465;
                    Planet(space.getChildAt(i)).pX *= -0.9;
                }
                else if(Planet(space.getChildAt(i)).x < 0){
                    Planet(space.getChildAt(i)).x = 0;
                    Planet(space.getChildAt(i)).pX *= -0.9;
                }
                if(Planet(space.getChildAt(i)).y > 465){
                    Planet(space.getChildAt(i)).y = 465;
                    Planet(space.getChildAt(i)).pY *= -0.9;
                }
                else if(Planet(space.getChildAt(i)).y < 0){
                    Planet(space.getChildAt(i)).y = 0;
                    Planet(space.getChildAt(i)).pY *= -0.9;
                }
                */
                Planet(space.getChildAt(i)).pX += (232.5-Planet(space.getChildAt(i)).x)/100;
                Planet(space.getChildAt(i)).pY += (232.5-Planet(space.getChildAt(i)).y)/100;
                
                if(!spaceBool && Planet(space.getChildAt(i)).blackhole) space.removeChildAt(i);
            }
            dx = mouseX-pvx;
            dy = mouseY-pvy;
            pvx=mouseX;
            pvy=mouseY;
            
            if(enterBool) createPlanet(mouseX,mouseY,1,0,0,false);    
        }
        
        private function gravity(a:Planet, b:Planet):void{
            var f:Number = (a.m*b.m)/((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
            var r:Number = Math.atan2(b.y-a.y,b.x-a.x);
            
            a.pX += 10*f*Math.cos(r);
            b.pX -= 10*f*Math.cos(r);
            a.pY += 10*f*Math.sin(r);
            b.pY -= 10*f*Math.sin(r);
            
        }

        
        private function createPlanet(ix:Number,iy:Number,im:Number,ipX:Number,ipY:Number,blackhole:Boolean):void{
            var p:Planet = new Planet(im,ipX,ipY,blackhole);
            p.x = ix;
            p.y = iy;
            space.addChild(p);
        }
        
        private function fusionPlanet(a:Planet,b:Planet):void{
            if(!a.blackhole && !b.blackhole){
                var distance:Number = distance(a,b);
            
                a.x += (b.x-a.x)*(b.m/(a.m+b.m));
                a.y += (b.y-a.y)*(b.m/(a.m+b.m));
            
                a.pX += b.pX;
                a.pY += b.pY;
            
                space.removeChild(b);
            
                a.massUp(b.m);
            }
            else if(a.blackhole){
                a.massUp(b.m);
                space.removeChild(b);
            }
            else{
                b.massUp(a.m);
                space.removeChild(a);
            }


        }

        
        private function mouseDown(e:MouseEvent):void{
            creating = true;
            mouseSx = mouseX;
            mouseSy = mouseY;
            mouseDx = mouseX - mouseSx;
            mouseDy = mouseY - mouseSy;
            mouseDownBool = true;
        }
        private function mouseMove(e:MouseEvent):void{
            if(mouseDownBool){
                mouseDx = mouseX - mouseSx;
                mouseDy = mouseY - mouseSy
            }
        }
        private function mouseUp(e:MouseEvent):void{
            this.createPlanet(mouseSx,mouseSy,planetScale,mouseDx/30,mouseDy/30,false);
            lineSpace.graphics.clear();
            mouseDownBool = false;
            planetScale = 1;
            creating = false;
        }
        private function keyUp(e:KeyboardEvent):void{
            if(e.keyCode == Keyboard.ENTER) enterBool = false;
            else if(e.keyCode == Keyboard.SPACE) spaceBool = false;
        }
        private function keyDown(e:KeyboardEvent):void{
            if(e.keyCode == Keyboard.ENTER) enterBool = true;
            else if(e.keyCode == Keyboard.SPACE) {
                spaceBool = true;
                createPlanet(mouseX,mouseY,300,0,0,true);
            }
        }

        
        private function distance(a:Planet,b:Planet):Number{
            return Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
        }


    }

}

import flash.display.Sprite;

internal class Planet extends Sprite {
    public var m:Number;
    public var pX:Number;
    public var pY:Number;
    public var r:Number;
    public var blackhole:Boolean;
    
    public function Planet(im:Number, ipX:Number, ipY:Number, iblackhole:Boolean){
        m=im;
        pX=ipX;
        pY=ipY;
        blackhole = iblackhole;
        if(!blackhole){
            r=Math.pow(im,1/3);
            graphics.beginFill(0xffffff);
            graphics.drawCircle(0,0,r);
        }
        else {
            r = 0;
        }

    }
    
    public function massUp(im:Number):void{
        m+=im;
        if(!blackhole){
            r=Math.pow(m,1/3);
            graphics.beginFill(0xffffff);
            graphics.drawCircle(0,0,r);
       }
    }
}