new year dragon

by xSonoSx forked from Button (diff: 190)
♥0 | Line 164 | Modified 2010-09-24 04:01:17 | MIT License
play

ActionScript3 source code

/**
 * Copyright xSonoSx ( http://wonderfl.net/user/xSonoSx )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/naVo
 */

package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Rectangle;
    import flash.text.*;
    import flash.geom.Point;
    import flash.utils.ByteArray;
    import flash.utils.Timer;

    public class Pixelation extends MovieClip
    {
        private var 
            dy:int = 0,
            canvasBMD:BitmapData = new BitmapData(465, 300, true, (0xFF << 24) + 0),
            tempBMD:BitmapData = new BitmapData(465, 300, true, (0xFF << 24) + 0),
            canvasBD:Bitmap = new Bitmap(canvasBMD, 'auto', true),
            holder:Sprite,
            txt:TextField,
            
            n:int = 0,
            
            drawableParticles:Vector.<Partikel> = new Vector.<Partikel>();            
        
        public function Pixelation():void {
            init();
      
            graphics.beginFill(0);
            graphics.drawRect(0,0,465,465);
            graphics.endFill();

            this.addEventListener(Event.ADDED_TO_STAGE, addedToStageListener);
        }
        
        private function init():void {
            holder = new Sprite();
            holder.x = holder.y = 0;
            this.addChild(holder);
            this.addChild(canvasBD);
            
            var format:TextFormat = new TextFormat();
            format.font = "Georgia";
            format.size = 20;
            
            txt = new TextField();
            txt.defaultTextFormat = format;
            txt.selectable = false;
            txt.width = 300;
            txt.height = 30;            
            txt.textColor = 0xFCB345;
            txt.text = "---->>>;press here;>>>>~";
            
            tempBMD.draw(holder);
            canvasBD.x =0;
            canvasBD.y = 0;
        }
        
        private function addedToStageListener(e:Event):void {
            this.removeEventListener(Event.ADDED_TO_STAGE, addedToStageListener);

            stage.addEventListener(MouseEvent.CLICK, click);
            redraw();
            this.swapChildren(holder, canvasBD);

            this.addEventListener(Event.ENTER_FRAME, redraw);
        }    
        
        private function click(e:MouseEvent = null):void {
            for (var i:int = 0; i < txt.textWidth; i+=5) 
            {
                for (var j:int = 0; j < txt.textHeight; j+=3) 
                {
                    if ((tempBMD.getPixel32(i, j)<<24) != 0) {
                        var rad:Number = 1 * Math.abs((90) * Math.cos((i + holder.rotationY) * Math.PI / 180));
                        addParticle(30, new Point((n-txt.textWidth/2)+i, (j+150)+10*Math.cos((i+n)/100*3.14)),0xE38A04, false,true);}
                }    
            }
            txt.text = "thank you";
        }
        
        private function redraw(e:Event = null):void { 
            var radius:int = 100;
            
            holder.rotationY = 45 * ((n - radius / 2 ) * 180 / (stage.stageWidth / 2) - 180) / 180;
            
            tempBMD.fillRect(getRect(canvasBD), (0x00<<24)+0);
            if (dy++ >= 0) {
                tempBMD.draw(txt);

                addParticle(1, new Point(Math.random() * stage.stageWidth,stage.stageHeight), 0x676767,true);
                for (var i:int = 0; i < txt.textWidth; ++i) 
                {
                    for (var j:int = 0; j < txt.textHeight; ++j) 
                    {
                        if ((tempBMD.getPixel32(i, j)<<24) != 0) {
                            var rad:Number = 1 * Math.abs((90) * Math.cos((i + holder.rotationY) * Math.PI / 180));
                            addParticle(1, new Point((n-txt.textWidth/2)+i, (j+150)+10*Math.cos((i+n)/100*3.14)),(((0xE3 - rad < 0)?0:0xE3 - rad) << 16) + (((0x8a - rad < 0)?0:0x8a - rad) << 8) + (((0x04 - rad < 0)?0:0x04 - rad) << 0),false,false);
                        }
                    }    
                }    
                dy = 0;
            }
            n += 3;
            if (n >= stage.stageWidth + txt.textWidth / 2) n = -txt.textWidth / 2;
            
            canvasBMD.fillRect(getRect(canvasBD), 0);
            
            if (drawableParticles.length > 0) {
                
                canvasBMD.lock();
                for each(var p:Partikel in drawableParticles) {
                    p.update();
                    canvasBMD.setPixel32(p.px, p.py, (p.a << 24) + p.c);
                    if ((p.a & 0xFF) - Partikel.ac < 0) { 
                        if(p.isRocket){
                            addParticle(12, new Point(p.px, p.py), Math.random()*0xFFFFFF, false,true);
                        }
                        drawableParticles.splice(drawableParticles.indexOf(p), 1);
                    }
                }
                canvasBMD.unlock();
            }
        }
        
        public function addParticle(amount:int,pos:Point,color:uint,rocket:Boolean=false,rp:Boolean=false):void {
            for (var i:int = 0; i < amount; ++i) 
            {
                var p:Partikel = new Partikel(color, pos.x+int(Math.random()*amount-amount/2), pos.y+int(Math.random()*amount-amount/2),rocket,rp);
                drawableParticles.push(p);
            }
        }    
    }
}

class Partikel {
        public static const ac:int =20;
        public var 
            n:int= 0,
            c:uint,
            a:uint=0xFF,
            px:Number,
            py:Number,
            vx:Number,
            vy:Number,

            rocketParticle:Boolean=false,    
            isRocket:Boolean = false;    
            
        public function Partikel(c:uint, px:Number, py:Number,rocket:Boolean=false,rocketParticle:Boolean=false) { 
            this.c = c;
            this.px = px;
            this.py = py ;
            this.isRocket = rocket;
            this.rocketParticle = rocketParticle;
            if(!rocketParticle){
                this.vx = (Math.random() * 2 - 1) * 50;
                this.vy = Math.random() * ac / 5;
            }else {
                if(rocketParticle){
                this.vx = (Math.random() * 2 - 1) * 1;
                this.vy = (Math.random() * 2 - 1) * 1;
                }
                if(rocket){
                this.vy = Math.random() * (ac / 5);
                }
            }
            this.a = a;
        }
        
        final public function update():void {
            if (!isRocket&&rocketParticle) {
                this.px += vx;
                this.py += vy;
                a -= 0x04;
            }
            if(!isRocket&&!rocketParticle){
                this.px += 1 * int(Math.sin(n++) * 100) / 100;
                if (a < 0xff - 1 * ac) this.py -= vy;
                a -= ac;
            }
            if (isRocket&&!rocketParticle) {
                this.py -= vy;
                a -= 0x03;
            }
        }
}