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

Langton Ants

A simple Langton Ants animation
Get Adobe Flash player
by tristow 11 Oct 2012
/**
 * Copyright tristow ( http://wonderfl.net/user/tristow )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/7TuC
 */

package {
    import flash.text.TextField;
    import flash.display.*;
    import flash.events.*;
    import net.hires.debug.Stats;
    [SWF(frameRate = "60")]
    public class FlashTest extends Sprite {
        public static const UP:int = 0;
        public static const RIGHT:int = 1;
        public static const DOWN:int = 2;
        public static const LEFT:int = 3;
        public static const BLACK:uint = 0xff000000;
        public static const WHITE:uint = 0xffffffff;
        public static const RED:uint = 0xffff0000;
        public var bitmap:BitmapData;
        public var textField:TextField;
        public var step:int = 0;
        public var currentX:int;
        public var currentY:int;
        public var currentDir:int;
        
        public function FlashTest() {
            bitmap = new BitmapData(stage.stageWidth/4,stage.stageHeight/4,false,WHITE);
            var b:Bitmap = addChild(new Bitmap(bitmap)) as Bitmap;
            b.width = stage.stageWidth;
            b.height = stage.stageHeight;
            
            currentX = bitmap.width/2;
            currentY = bitmap.height/2;
            currentDir = UP;
            textField = new TextField;
            textField.x = 200;
            addChild(textField);
            //addChild(new Stats());
            addEventListener(Event.ENTER_FRAME,onEnterFrame);
        }
                
        public function onEnterFrame(e:Event):void{
            new BitmapData(currentX,currentY,0xff0000);
            if (currentX>=0&&currentX<bitmap.width&&
                currentY>=0&&currentY<bitmap.height)
            {
                
                if(bitmap.getPixel32(currentX,currentY)==WHITE)
                {
                    bitmap.setPixel(currentX,currentY,BLACK);
                    currentDir = (currentDir + 3)%4;
                }
                else
                {
                    bitmap.setPixel(currentX,currentY,WHITE);
                    currentDir = (currentDir + 1)%4;
                }
                if (currentDir==UP)
                    ++currentY;
                else if (currentDir==DOWN)
                    --currentY;
                else if (currentDir==LEFT)
                    ++currentX;
                else if (currentDir==RIGHT)
                    --currentX;
                ++step;
                textField.text=step.toString();


            }
        }
    }
}