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

generate random maps with perlin

Get Adobe Flash player
by Albert 20 May 2011
/**
 * Copyright Albert ( http://wonderfl.net/user/Albert )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/64z0
 */

//From http://www.emanueleferonato.com/2011/05/17/using-cellular-automata-to-generate-random-land-and-water-maps-with-flash/
// copy-paste h_seldon comment

package {
    import flash.display.AVM1Movie;
    //——————- import ———————-
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.geom.Rectangle;
    import flash.geom.Point;
    import flash.events.Event;    
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    
    public class FlashTest extends Sprite {
        //——————- vars ———————-
        protected var bmTerrain:Bitmap;
        protected var bmpTerrain:BitmapData;
        protected var freqDivisor:Number = 0.5;
        protected var octs:Number = 8;
        protected var seed:Number = 12;
        protected var stitch:Boolean = true;
        protected var fractal:Boolean = true;
        protected var channels:Number = 1;
        protected var gray:Boolean = true;
        protected var red:Array = new Array(256);
        protected var green:Array = new Array(256);
        protected var blue:Array = new Array(256);
        protected var land:Number = 0xff00ff00;
        protected var water:Number = 0xff0000ff;   
        
        //——————- start ———————-            
        public function FlashTest() {
            // write as3 code here..
            init();
            stage.addEventListener(MouseEvent.CLICK, reset);
        }

        //——————- functions ———————-
        public function init():void {
            for (var i:Number = 0; i < 128; i++) {
                red[i]=green[i]=blue[i]=land;
            }
            for (i = 128; i < 256; i++) {
                red[i]=green[i]=blue[i]=water;
            }
            bmpTerrain = new BitmapData(800,600);
            bmpTerrain.perlinNoise(bmpTerrain.width*freqDivisor,bmpTerrain.height*freqDivisor,octs,seed,stitch,fractal,channels,gray);
            bmpTerrain.paletteMap(bmpTerrain,new Rectangle(0,0,bmpTerrain.width,bmpTerrain.height),new Point(0,0),red,green,blue);
            bmTerrain = new Bitmap(bmpTerrain);
            addChild(bmTerrain);
        }

        public function destroy():void {
            removeChild(bmTerrain);
            bmTerrain=null;
            bmpTerrain=null;
        }
 

        public function reset(e: MouseEvent=null):void {
            destroy();
            seed = Math.random()*2000;
            init();
        }
 
     
    }
}