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: Black Hole

Get Adobe Flash player
by whodatis 17 Jun 2011
// forked from joeRob2468's Black Hole
package
{
    
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    
    import flash.geom.Point;
    import flash.geom.Rectangle;
    
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.KeyboardEvent;
    
    import flash.ui.Keyboard;
    
    import flash.filters.BlurFilter;
    import flash.filters.ColorMatrixFilter;
    
    import flash.text.TextField;
    
    public class Main extends MovieClip
    {
        
        private var bmd:BitmapData;
        private var bmp:Bitmap;
        
        private var bmpHolder:Sprite;
        
        private var particleArray:Array;
        
        private var blur:BlurFilter;
        private var color:ColorMatrixFilter;
        
        private var textField:TextField;
        
        public var particleScale:Number = 5;
        
        public function Main()
        {
            init();
        }
        
        public function init():void
        {
            particleArray = new Array();
            bmd = new BitmapData(stage.stageWidth,stage.stageHeight,false,0x000000);
            bmp = new Bitmap(bmd);
            
            bmpHolder = new Sprite();
            addChild(bmpHolder);
            bmpHolder.addChild(bmp);
            
            textField = new TextField();
            textField.width = 100;
            textField.textColor = 0xFFFFFF;
            textField.selectable = false;
            addChild(textField);
            
            blur = new BlurFilter(2,2,3);
            var fade:Number = .85;
            color = new ColorMatrixFilter([fade,0,0,0,0,
                                           0,fade+.05,0,0,0,
                                           0,0,fade+.1,0,0,
                                           0,0,0,fade,0]);
            
            addEventListener(Event.ENTER_FRAME,draw);
            createParticles();
        }
        
        private function createParticles(e:MouseEvent=null):void
        {
            for(var sx:Number=0;sx<stage.stageWidth/particleScale;sx++)
            {
                for(var sy:Number=0;sy<stage.stageHeight/particleScale;sy++)
                {
                    var particle:Particle = new Particle(sx*particleScale,sy*particleScale,stage);
                    particleArray.push(particle);
                }
            }
        }
        
        private function draw(e:Event):void
        {
            bmd.unlock();
            //bmd.fillRect(bmd.rect,0x000000);
            
            var n:Number = 0;
            
            for each(var particle:Particle in particleArray)
            {
                n++;
                particle.update(mouseX,mouseY);
                bmd.setPixel(particle.x,particle.y,particle.color);
                /*for(var sx:Number=0;sx<particle.size;sx++)
                {
                    for(var sy:Number=0;sy<particle.size;sy++)
                    {
                        bmd.setPixel(particle.x+sx,particle.y+sy,particle.color);
                    }
                }*/
            }
            bmd.applyFilter(bmd,bmd.rect,new Point(0,0),blur);
            bmd.applyFilter(bmd,bmd.rect,new Point(0,0),color);
            bmd.lock();
            textField.text = n.toString()+" particles";
        }
    }
}
import flash.events.Event;
import flash.display.Stage;
    
class Particle
{
    
    public var color:uint = 0xFFFFFF;
    
    public var vx:Number = 0;
    public var vy:Number = 0;
    
    public var x:Number = 0;
    public var y:Number = 0;
    
    public var friction:Number = .99;
    
    public var gravity:Number = 1;
    
    public var particleArray:Array;
    
    public var stageRef:Stage;
    
    public var size:Number = 2;

    public function Particle(x:Number,y:Number,stageRef:Stage,vx:Number = 0,vy:Number = 0)
    {
        this.x = x;
        this.y = y;
        this.stageRef = stageRef;
        this.vx = vx;
        this.vy = vy;
        
        init();
    }
    
    private function init():void
    {
        
    }
    
    public function update(tx:Number,ty:Number):void
    {
        var dx:Number = x-tx;
        var dy:Number = y-ty;
        var d:Number = Math.sqrt(dx*dx+dy*dy);
        
        var modifier:Number = this.gravity;
        modifier -= 1.5/d;
        
        var fx:Number = (modifier/d)*dx;
        var fy:Number = (modifier/d)*dy;
        
        vx -= fx;
        vy -= fy;
        
        vx *= friction;
        vy *= friction;
        
        x += vx;
        y += vy;
        
        if(x<0)
        {
            x = 0;
            vx *= -1;
        }
        if(y<0)
        {
            y = 0;
            vy *= -1;
        }
        if(x>stageRef.stageWidth)
        {
            x = stageRef.stageWidth;
            vx *= -1;
        }
        if(y>stageRef.stageHeight)
        {
            y = stageRef.stageHeight;
            vy *= -1;
        }
    }
    
    public function destruct():void
    {
        for(var i:Number=0;i<particleArray.length;i++)
        {
            if(particleArray[i]==this)
            {
                particleArray.splice(i,1);
                return;
            }
        }
    }
    
}