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

flash on 2011-6-26

Get Adobe Flash player
by kihon 26 Jun 2011
    Embed
package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.net.URLRequest;
    import flash.system.LoaderContext;
 
    public class Main extends Sprite
    {
        private const WIDTH:int = 15;
        private const HEIGHT:int = 15;
        private const SIZE:int = 32;
        private var map:Array = [];
        private var tile:BitmapData;
        private var canvas:BitmapData;
        private var loadCount:int = 0;
        private var images:Array = [];
        private const URL:Array = 
        [
            "http://assets.wonderfl.net/images/related_images/e/ed/ed0f/ed0f40f511ad2635c8736827fc62bc475e583ec8",
            "http://assets.wonderfl.net/images/related_images/d/dd/ddf0/ddf0405a0c2a82f7a5fed6d28578cde2d2a693bc"
        ];
 
        public function Main()
        {
            for (var i:int = 0; i < URL.length; i++)
            {
                var loader:Loader = new Loader();
                loader.name = i.toString();
                loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
                loader.load(new URLRequest(URL[i]), new LoaderContext(true));
            }
        }
        
        private function initHandler(event:Event):void
        {
            var loader:Loader = event.currentTarget.loader as Loader;
            var bd:BitmapData = new BitmapData(loader.width, loader.height, true, 0x0);
            bd.draw(loader);
            images[int(loader.name)] = bd;
            if (++loadCount == URL.length) init();
        }
        
        private function init():void
        {
            var chip:BitmapData = images[1];
            graphics.beginBitmapFill(chip);
            graphics.drawRect(0, 0, 465, 465);
            graphics.endFill();
            
            canvas = new BitmapData(WIDTH * SIZE, HEIGHT * SIZE, true, 0x0);
            var bitmap:Bitmap = new Bitmap(canvas);
            addChild(bitmap);
 
            var bd:BitmapData = images[0];
            tile = new BitmapData(SIZE * 6, SIZE, true, 0x0);
            tile.copyPixels(bd, new Rectangle(0, 0, SIZE, SIZE), new Point(SIZE, 0));
            tile.copyPixels(bd, new Rectangle(SIZE, 0, SIZE, SIZE), new Point(SIZE * 4, 0));
            tile.copyPixels(bd, new Rectangle(SIZE / 2, SIZE + SIZE / 2, SIZE, SIZE), new Point(SIZE * 5, 0));
            
            tile.copyPixels(bd, new Rectangle(SIZE / 2, SIZE, SIZE, SIZE / 2), new Point(SIZE * 2, 0));
            tile.copyPixels(bd, new Rectangle(SIZE / 2, SIZE * 2 + SIZE / 2, SIZE, SIZE / 2), new Point(SIZE * 2, SIZE / 2));
            
            tile.copyPixels(bd, new Rectangle(0, SIZE + SIZE / 2, SIZE / 2, SIZE), new Point(SIZE * 3, 0));
            tile.copyPixels(bd, new Rectangle(SIZE + SIZE / 2, SIZE + SIZE / 2, SIZE / 2, SIZE), new Point(SIZE * 3 + SIZE / 2, 0));
            
            for (var y:int = 0; y < HEIGHT; y++)
            {
                map[y] = [];
                for (var x:int = 0; x < WIDTH; x++)
                {
                    map[y][x] = (Math.random() < 0.7) ? 1 : 0;
                }
            }
            
            for (y = 0; y < HEIGHT; y++)
            {
                for (x = 0; x < WIDTH; x++)
                {
                    update(x, y);
                }
            }
 
            stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
            stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
        }
        
        private function onMouseUp(event:MouseEvent):void 
        {
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
        }
 
        private function onMouseDown(event:MouseEvent):void 
        {
            stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
            onMouseMove(event);
        }
        
        private function onMouseMove(event:MouseEvent):void 
        {
            var ty:int = mouseY / SIZE;
            var tx:int = mouseX / SIZE;
 
            if (getMapValue(tx, ty) == -1) return;
 
            if (event.shiftKey) map[ty][tx] = 0;
            else map[ty][tx] = 1;
            update(tx, ty);
        }
        
        private function getMapValue(tx:int, ty:int):int
        {
            if (ty < 0 || HEIGHT <= ty ||
                tx < 0 || WIDTH <= tx) return -1;
            
            return map[ty][tx];
        }
 
        private function update(tx:int, ty:int):void
        {
            for (var y:int = 0; y < 4; y++)
            {
                for (var x:int = 0; x < 4; x++)
                {
                    var vy:int = (y % 2) ? -1 : 1;
                    var vx:int = (x % 2) ? -1 : 1;
                    var py:int = (ty * SIZE + (y - 1) * SIZE / 2) / SIZE;
                    var px:int = (tx * SIZE + (x - 1) * SIZE / 2) / SIZE;
                    var my:int = getMapValue(px, py);
                    var ay:int = getMapValue(px, py + vy);
                    var ax:int = getMapValue(px + vx, py);
                    var ac:int = getMapValue(px + vx, py + vy);
                    
                    var index:int;
                    if (my == 1)
                    {
                        if (my == ax && my == ay && my == ac) index = 5;
                        else if (my == ax && my == ay) index = 4;
                        else if (my == ay) index = 3;
                        else if (my == ax) index = 2;
                        else index = 1;
                    }
                    else index = 0;
                    
                    var by:int = (y % 2 == 0) ? 1 : 0;
                    var bx:int = (x % 2 == 0) ? 1 : 0;
                    
                    canvas.copyPixels(tile, new Rectangle(SIZE * index + bx * SIZE / 2, by * SIZE / 2, SIZE / 2, SIZE / 2), new Point(tx * SIZE + (x - 1) * SIZE / 2, ty * SIZE + (y - 1) * SIZE / 2));
                }
            }
        }
    }
}