Simple Cellular Automata Example in AS3
To get a little more experience with ActionScript 3, I wrote up a very simple class which will take a rule number, and then generate a cellular automata based on that.
The movie below requires Flash 9 or above to be installed. Some rules to try out: 90, 30, 73, 110, 150. Valid numbers are 0-255, but not all produce interesting results.
This movie requires a newer Flash Player version
Direct link to flash movie (got some people complaining the SWFObject script doesn't work under Vista)
See Wikipedia for more info, and Wolfram.com has a list of all 256 possible results
Here's the code:
Actionscript:
-
/**
-
* Simple Cellular Automata example
-
* by David Rorex, 2008
-
*/
-
package
-
{
-
import flash.display.*;
-
import flash.events.*;
-
import flash.geom.*;
-
import flash.text.*;
-
import flash.ui.Keyboard;
-
import flash.utils.getTimer;
-
import sim.EasyButton;
-
-
public class Main extends Sprite
-
{
-
/// Holds the data we draw onto
-
private var bdata:BitmapData;
-
-
/// Bitmap container for displaying our rendering
-
private var bitmap:Bitmap;
-
-
/// Text field for typing in rule numbers
-
private var inp:TextField;
-
-
/// Button to click to render
-
private var btn:EasyButton;
-
-
/// Constructor
-
public function Main():void
-
{
-
// set up the input textfield
-
inp = new TextField();
-
addChild(inp);
-
inp.type = TextFieldType.INPUT;
-
inp.border = true;
-
inp.text = "90";
-
inp.addEventListener(KeyboardEvent.KEY_UP, onEnter);
-
inp.height = 20;
-
-
// set up the render button
-
btn = new EasyButton("Render");
-
btn.addEventListener(MouseEvent.CLICK, function(e:*):void { doRender(); } );
-
addChild(btn);
-
-
// set the stage to not scale, and to get an event if the window changes size
-
stage.scaleMode = StageScaleMode.NO_SCALE;
-
stage.align = StageAlign.TOP_LEFT;
-
stage.addEventListener(Event.RESIZE, onResize)
-
-
// initialize the size
-
setSize(stage.stageWidth, stage.stageHeight);
-
}
-
-
/**
-
* Event handler for when the stage changes size
-
* @param e
-
*/
-
private function onResize(e:Event):void {
-
setSize(stage.stageWidth, stage.stageHeight);
-
}
-
-
/**
-
* Event handler to catch the user pressing ENTER in the text field
-
* @param e
-
*/
-
public function onEnter(e:KeyboardEvent):void {
-
if (e.charCode == Keyboard.ENTER)
-
doRender();
-
}
-
-
/**
-
* Simple method to call the render function
-
*/
-
public function doRender():void {
-
render(parseInt(inp.text));
-
}
-
-
/**
-
* Adjust the positioning of our elements based on a given size
-
* @param w
-
* @param h
-
*/
-
public function setSize(w:Number, h:Number):void {
-
btn.x = w - btn.width;
-
inp.width = w - btn.width - 2;
-
}
-
-
/**
-
* Render cellular automata.
-
* @param ruleNum - the number of the rule to draw. Some interesting numbers are '30' and '90'
-
*/
-
public function render(ruleNum:int):void {
-
-
// clean up previous bitmap
-
if (bitmap != null) {
-
removeChild(bitmap);
-
bitmap = null;
-
}
-
if (bdata != null) {
-
bdata.dispose();
-
bdata = null
-
}
-
-
// get width and height of the stage
-
var w:Number = stage.stageWidth;
-
var h:Number = stage.stageHeight - 20;
-
-
// build lookup table based on the rule number
-
var rule:Array = [];
-
for (var i:int = 0; i <8; i++)
-
rule[i] = (ruleNum>> i) & 1;
-
-
// create bitmap data that we will draw into
-
bdata = new BitmapData(w, h, false, 0);
-
-
// lock the bitmap data, provides speedups when you are doing a lot of manipulations
-
bdata.lock();
-
-
// set the starting data - a single pixel in the center
-
bdata.setPixel(Math.round(w/2), 0, 1);
-
-
// loop row by row
-
for (var y:int = 1; y <h; y++)
-
// loop pixel by pixel
-
for (var x:int = 0; x <w; x++) {
-
-
// use lookup table based up 3 neighboring pixels to calculate new pixel
-
bdata.setPixel(x, y,
-
rule[bdata.getPixel(x - 1, y - 1) <<2
-
| bdata.getPixel(x , y - 1) <<1
-
| bdata.getPixel(x + 1, y - 1)]);
-
-
}
-
-
// map the pixels, 0-white, 1-black
-
bdata.paletteMap(bdata,
-
new Rectangle(0, 0, w, h), new Point(0, 0),
-
[], [], [0xffffff, 0]);
-
-
bdata.unlock(); // unlock the bitmap data
-
-
// create the bitmap to hold the data
-
bitmap = new Bitmap(bdata, PixelSnapping.ALWAYS, false);
-
-
bitmap.y = 21;
-
-
addChildAt(bitmap,0); // add it to our display list so we can see it
-
}
-
}
-
}

looks like your flash detection is broken for FP10/FF/vista
Comment by felix — August 22, 2008 @ 12:40 pm
It works for me with FP10/FF/XP…dunno why vista would would make a difference. It’s using swfobject which is supposed to be pretty good about that sort of thing. I can’t be bothered to debug a flash detection script for vista, so I added a direct link to the swf in the post.
Comment by davr — August 22, 2008 @ 1:03 pm
[...] public links >> positioning Simple Cellular Automata Example in AS3 Saved by RiN on Wed 22-10-2008 Advising Agencies That Get Positioning Saved by andrewwilles on [...]
Pingback by Recent Links Tagged With "positioning" - JabberTags — October 23, 2008 @ 5:04 am