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

Understand Conway' s Game of Life

...
@author ZhouTing
Get Adobe Flash player
by zotin95 14 Apr 2010
/**
 * Copyright zotin95 ( http://wonderfl.net/user/zotin95 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/29xu
 */

package 
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Point;
	import flash.filters.*;
	/**
	 * ...
	 * @author ZhouTing
	 */
	public class Main extends Sprite 
	{
		protected var bmpData : BitmapData;
		protected var storedBmpData : BitmapData;
		protected var canvas : Bitmap;
		protected var grid : Shape;
		protected var bw : int;
		protected var stageW : Number;
		protected var stageH : Number;
		protected var vw : int;
		protected var pixelValueTexts : Array;
		protected var pt0 : Point =  new Point();
		protected var step : int = 0;
		
		[SWF(width = "465", height = "465", backgroundColor = "#000000")]
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			stageW = stage.stageWidth;
			stageH = stage.stageHeight;
			bw = 35;
			
			bmpData = new BitmapData(bw,bw,false,0);
			canvas = new Bitmap(bmpData);
			canvas.width = stageW;
			canvas.height = stageH;
			
			addChild(canvas);
			var patterns : Array = ["*.*","*.*","***"];
			var x0 : int = int(bw / 2 - 1);
			var y0 : int = int(bw / 2 - 3);
			for (var i : int = 0; i < patterns.length; i++)
			{
				for (var k:int = 0; k < patterns[i].length; k++)
				{
					if(patterns[i].charAt(k) == "*") {
						var px : int = x0 + k;
						var py : int = y0 + i;
						bmpData.setPixel(px,py,0xff0000);
					}
				}
			}
			grid = new Shape();
			var g : Graphics = grid.graphics;
			var cw : Number = stageW / bw;
			g.lineStyle(1,3092271);
			for (var d :int = 0; d < bw; d++)
			{
				g.moveTo(0,d * cw);
				g.lineTo(stageW,d * cw);
				g.moveTo(d * cw,0);
				g.lineTo(d * cw,stageW);
			}
			addChild(grid);
			storedBmpData = bmpData.clone();
			stage.addEventListener(MouseEvent.CLICK,nextStep);
		}
		protected function nextStep(e : Event) : void {
			switch(step % 2) {
			case 0:{
				applyConvlutionFilter();
			}break;
			case 1:{
				doPaletteMap();
			}break;
			default:{
				return;
			}break;
			}
			++step;
			if (step > 10)
			{
				stage.removeEventListener(MouseEvent.CLICK,nextStep);
				stage.addEventListener(Event.ENTER_FRAME,nextStep);
			}
		}
		
		protected function applyConvlutionFilter() : void {
			var filter : ConvolutionFilter = new ConvolutionFilter(3,3,[1,1,1,1,9,1,1,1,1],255);
			bmpData.applyFilter(bmpData, bmpData.rect, pt0, filter);
			storedBmpData.dispose();
			storedBmpData = bmpData.clone();
			var disp : Array = [
				0, 0, 0, 0xff6611,
				0, 0, 0, 0,
				0, 0x6f0000, 0x6f0000, 0xff0000, 
				0xff0000, 0x6f0000, 0x6f00000,
				0x6f0000, 0x6f0000
			];
			bmpData.paletteMap(bmpData,bmpData.rect,pt0,disp);
		}
		
		protected function doPaletteMap() : void {
			var ruleR : Array = [0,0,0,16711680,0,0,0,0,0,0,0,16711680,16711680];
			var ruleG : Array = [0,0,0,16711680,0,0,0,0,0,0,0,16711680,16711680];
			bmpData.paletteMap(storedBmpData,bmpData.rect,pt0,ruleR,null,null);
			storedBmpData.dispose();
			storedBmpData = bmpData.clone();
		}
	}
	
}