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

Bitmap Destruction

Get Adobe Flash player
by Abarrow 03 Jun 2011
package {
    import flash.display.*;
    import flash.events.*;
    
    public class FlashTest extends Sprite
    {
        public function FlashTest()
        {
            this.newland();
            this.mx = 0;
            this.my = 0;
            this.delay = 0;
            this.md = false;
            this.shots = new Vector.<Shot>();
            this.particles = new Array();
            addChild(this.ground);
            this.hero = new Sprite();
            this.hero.graphics.lineStyle(1);
            this.hero.graphics.lineTo(-6, 0);
            this.hero.x = stage.stageWidth / 2;
            addChild(this.hero);
            addEventListener(Event.ENTER_FRAME, this.enterframe);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.mousemove);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, this.mousedown);
            stage.addEventListener(MouseEvent.MOUSE_UP, this.mouseup);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void{paused=!paused;});
        }
        public var mx:int;
        public var my:int;
        public var md:Boolean;
        public var ground:Bitmap;
        public var land:BitmapData;
        public var hero:Sprite;
        public var shots:Vector.<Shot>;
        public var particles:Array;
        public var delay:Number;
        public var paused:Boolean=false;
        public var highest:int;
        public var continu:Boolean;

        private function mousedown(event:MouseEvent):void{
            this.md = true;
        }
        private function mouseup(event:MouseEvent):void{
            this.md = false;
        }
        private function mousemove(event:MouseEvent):void{
            this.mx = event.stageX;
            this.my = event.stageY;
        }
        public function enterframe(arg1:flash.events.Event):void
        {
            if(!paused){
                var n:int=0;
                var bull:Shot=null;
                var col:*=NaN;
                var ex:*=0;
                var ey:*=0;
                var bellow:uint=0;
                var curr:uint=0;
                var hor:uint=0;
                //aim
                var dx:Number=this.hero.x - this.mx;
                var dy:Number=this.hero.y - this.my;
                var ang:Number=Math.atan2(dy, dx);
                this.hero.rotation = ang / Math.PI * 180;
                //reload
                this.delay--;
                //fire
                if (this.delay <= 0 && this.md)
                {
                    this.delay = 5;
                    bull = new Shot();
                    //col = Math.random() * Math.pow(2, 24);
                    col=0;
                    bull.graphics.lineStyle(1, col);
                    bull.graphics.beginFill(col);
                    bull.graphics.drawCircle(0, 0, 1);
                    bull.graphics.endFill();
                    bull.vx = Math.cos(ang) * 4;
                    bull.vy = Math.sin(ang) * 4;
                    bull.x = this.hero.x - bull.vx * 3;
                    bull.y = this.hero.y - bull.vy * 3;
                    addChild(bull);
                    this.shots.push(bull);
                }
                //moves the bullets
                while (n < this.shots.length) 
                {
                    this.shots[n].x = this.shots[n].x - this.shots[n].vx;
                    this.shots[n].y = this.shots[n].y - this.shots[n].vy;
                    if (this.hitTest(this.shots[n].x, this.shots[n].y))
                    {
                        ex = -20;
                        while (ex < 21) 
                        {
                            ey = -20;
                            while (ey < 21) 
                            {
                                if (Math.sqrt(ex * ex + ey * ey) < 20)
                                {
                                    this.land.setPixel32(this.shots[n].x + ex, this.shots[n].y + ey, 0);
                                }
                                ++ey;
                            }
                            ++ex;
                        }
                        removeChild(this.shots[n]);
                        this.shots.splice(n, 1);
                    }    
                    else 
                    {
                        if (this.shots[n].x + this.shots[n].width < 0 || this.shots[n].x - this.shots[n].width > stage.stageWidth || this.shots[n].y + this.shots[n].height < 0 || this.shots[n].y - this.shots[n].height > stage.stageHeight)
                        {
                            removeChild(this.shots[n]);
                            this.shots.splice(n, 1);
                        }
                    }
                    n++;
                }
                //allow pixels to fall
                this.land.lock();
                var hi:int=highest+0;
                if(continu){
                    hi=land.height-1;
                }
                for(ey=land.height-1;ey>highest;ey--){
                    for(ex=0;ex<this.land.width;ex++){
                        curr=this.land.getPixel32(ex, ey);
                        if(curr==0){
                            //air
                            continue;
                        }else{
                            //gravity
                            if(ey<hi&&continu){
                                hi=ey;
                            }
                            if(ey!=this.land.height-1){
                                bellow=this.land.getPixel32(ex,ey+1);
                                if(bellow==0){
                                    //switch spots
                                    this.land.setPixel32(ex,ey,bellow);
                                    this.land.setPixel32(ex,ey+1,curr);
                                    continue;
                                }else{
                                    var above:int=this.land.getPixel(ex,ey-1);
                                    if(above==0){
                                        continue;
                                    }else{
                                        var nex:int=ex+Math.floor(Math.random()*7)-3;
                                        hor=this.land.getPixel32(nex, ey);
                                        if(hor!=0||nex<0||nex>this.land.width){
                                            continue;
                                        }else{
                                            this.land.setPixel32(ex,ey,hor);
                                            this.land.setPixel32(nex,ey,curr);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                continu=false;
                highest=hi;
                this.land.unlock();
                /*graphics.clear();
                graphics.lineStyle(1,0);
                graphics.moveTo(0,highest);
                graphics.lineTo(100,highest);*/
            }
        }

        
        public function hitTest(ex:int, ey:int, al:int=128):Boolean
        {
            if ((this.land.getPixel32(ex, ey) >> 24 & 255) >= al)
            {
                return true;
            }else{
                return false;
            }
        }

        public function newland():void
        {
            this.land = new flash.display.BitmapData(625, 625, true, 0);
            highest=0;
            continu=true;
            this.ground = new flash.display.Bitmap(this.land);
            var h:*=300;
            var r:int=128;
            var g:int=128;
            var b:int=128;
            var ex:int=0;
            var ey:int=0;
            while (ex < 625) 
            {
                h += Math.round(Math.random() * 3) * this.pum();
                r +=Math.round(Math.random() * 3) * this.pum();
                g +=Math.round(Math.random() * 3) * this.pum();
                b +=Math.round(Math.random() * 3) * this.pum();
                if (r > 255)
                {
                    r = 255;
                }
                if (g > 255)
                {
                    g = 255;
                }
                if (b > 255)
                {
                    b = 255;
                }
                if (r < 0)
                {
                    r = 0;
                }
                if (g < 0)
                {
                    g = 0;
                }
                if (b < 0)
                {
                    b = 0;
                }
                ey = 625;
                while (ey > h) 
                {
                    this.land.setPixel32(ex, ey, 255 * Math.pow(2, 24) + r * Math.pow(2, 16) + g * Math.pow(2, 8) + b);
                    ey--;
                }
                ++ex;
            }
        }

        private function pum():int{
            if (Math.random() > 0.5)
            {
                return 1;
            }
            return -1;
        }
    }
}

import flash.display.MovieClip;
class Shot extends MovieClip{
    public var vx:Number;
    public var vy:Number;
}