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: Pixel Perfect Particles with Basic Physics

@author duarte peixinho
mail: duarte.peixinho@gmail.com
Pixel Perfect Particles with Basic Physics
// forked from peixinho's Pixel Perfect Particles with Basic Physics
package
{
    import flash.system.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.net.*;
    import flash.text.*;
    import flash.display.*;
    
    /**
     * @author duarte peixinho
     * mail: duarte.peixinho@gmail.com
     * Pixel Perfect Particles with Basic Physics
     */
    
    [SWF(width = "600", height = "400", backgroundColor = "#000000", frameRate = "60")]     
    
    public class Main extends Sprite 
    {
    
        private var loader:Loader = new Loader();
        private var imageSourceBitmapData:BitmapData;
        private var imageSource:Bitmap;
        private var mouseClicked:Boolean = false;
        private var Pixels:Array = new Array();
        private var text:TextField = new TextField();
        
        public function Main():void 
        {
            stage.quality = StageQuality.LOW;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            
                        Security.loadPolicyFile("http://duartepeixinho.comcrossdomain.xml");
                        Security.allowDomain("duartepeixinho.com");
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
                            
                        var loaderContext:LoaderContext = new LoaderContext();
                        loaderContext.checkPolicyFile = true;

            loader.load(new URLRequest("http://duartepeixinho.com/flash/bitmapdata1/logo.png"),loaderContext);

                        text.antiAliasType = AntiAliasType.ADVANCED;
            text.blendMode = BlendMode.LAYER;
            text.x = stage.stageWidth/2-text.width/2;
            text.y = stage.stageHeight/2;
            text.autoSize = TextFieldAutoSize.LEFT;
            text.setTextFormat(new TextFormat("Arial", 14, 0x303030, true));
            text.textColor = 0xffffff;
            text.selectable = false;
                        text.text="Loading Image...";
                        addChild(text);
                
        }
        private function onLoadComplete(e:Event):void {
            
            imageSource = Bitmap(e.target.loader.content);
            imageSourceBitmapData = imageSource.bitmapData;

                        // -------------------- creating scene -------------------------
                        removeChild(text);
                        text.text = "Click on the Screen";
            text.x = (stage.stageWidth) - text.width -10;
            text.y = stage.stageHeight - 30;            
            text.alpha = 0;
            addChild(text);

                        //enter frame
            addEventListener(Event.ENTER_FRAME, processframe);    
            stage.addEventListener(MouseEvent.CLICK, mouseClick);
            
            //--------------------- creating the pixels ------------------------            
            var l:int = 0;
            for (var i:int = 0; i < imageSource.width; i++)
                for (var k:int = 0; k < imageSource.height; k++) {                            
                    var pixelB:BitmapData = new BitmapData(1, 1);
                    pixelB.copyPixels(imageSourceBitmapData, new Rectangle(i, k, i + 1, k + 1), new Point(0, 0));
                    var Pixel:pixels = new pixels(this.stage,new Bitmap(pixelB), Math.round(stage.stageWidth/2 - imageSource.width / 2 + i),Math.round(stage.stageHeight / 2 - imageSource.height / 2 + k),Math.random() *6 - 3,Math.random() *-5-10,Math.round(Math.random()*100));
                    Pixels.push(Pixel);
                    addChild(Pixels[l].show());
                    l++;                                    
                }
            
        }
        private function mouseClick(e:MouseEvent):void {
            if (!mouseClicked) mouseClicked = true;
            else mouseClicked = false;
        }
        
        private function processframe(e:Event):void 
        {
            //enter frame function
            //textfield
            if (text.alpha < 1) text.alpha += 0.01;
            
            for (var i:int = 0; i < Pixels.length; i++)    {
                    Pixels[i].update();
                    if (Pixels[i].moving != mouseClicked) {
                        Pixels[i].moving = mouseClicked;                    
                        if (mouseClicked) {
                            Pixels[i].animation++;
                            Pixels[i].resetValues();
                        }
                        if (Pixels[i].animation == 5) Pixels[i].animation = 1;
                    }
            }
        }
                
    }
    
}
    import flash.display.Bitmap;
    import flash.display.Stage;
    
    class pixels 
    {
        private var XposInit:Number;
        private var YposInit:Number;
        private var Xvel:Number;
        private var YvelInit:Number;
        private var Yvel:Number;
        
        //resize
        private var posXstage:Number;
        private var posYstage:Number;
        
        private var pixel:Bitmap;
        private var grav:Number = 0.98;
        private var time:Number;
        private var timeInit:Number;
        public var moving:Boolean = false;
        private var stage:Stage;
        //animations
        public var animation:uint = 0;
        
        public function pixels(stage:Stage, pixelB:Bitmap, x:Number, y:Number, XvelParam:Number = 0, YvelParam:Number = 0, timeParam:Number = 0):void
        {
            pixel = pixelB;
            XposInit = x;
            YposInit = y;            
            pixel.x = x;
            pixel.y = y;
            Xvel = XvelParam;
            Yvel = YvelParam;
            YvelInit = YvelParam;
            time = timeParam;            
            timeInit = timeParam;
            this.stage = stage;
            posXstage = pixel.x - stage.stageWidth / 2;
            posYstage = pixel.y - stage.stageHeight / 2;
        }
        
        public function show():Bitmap {        
            return pixel;
        }
        public function resetValues():void {
            //clear other animations
            time = timeInit;
            Yvel = YvelInit;
        }
        public function update():void {        
            
            // ----------------------------- resize ---------------- //
            if (XposInit - stage.stageWidth/2 != posXstage) {
                XposInit = Math.round(stage.stageWidth / 2 + posXstage);
            }
            if (pixel.y - stage.stageHeight != posYstage) {
                YposInit = Math.round(stage.stageHeight / 2 + posYstage);
            }
            
            if (moving) {
                //------------------------- animations here --------------------- //
                switch(animation) {
                case 1:            
                    pixel.x = pixel.x + Math.random() * 6 - 3;
                    pixel.y = pixel.y + Math.random() * 6 - 3;
                    break;
                case 2:
                    if (time == 0) {
                        if (Yvel>0) pixel.y += grav +Yvel;
                        else pixel.y += grav -Yvel;
                    }
                    else time--;
                    break;                        
                case 3:
                case 4:
                    pixel.x += Xvel;
                    pixel.y += Yvel;
                    Yvel += grav;
                    break;            
                }
                // -------------- physics here ----------------- //
                if ((animation == 3 || animation == 4) && pixel.y > stage.stageHeight) {
                    Yvel = Yvel * -0.7;
                    pixel.y = -Math.random() * Yvel  +stage.stageHeight;
                    if (animation == 4) {
                        pixel.x = stage.stageWidth/2;
                    }
                }
            }
            else {
                pixel.x += (XposInit - pixel.x) / 4;
                pixel.y += (YposInit - pixel.y) / 4;
            }            
        }
        
    }