Random Walk Texture

Actionscript:
  1. package {
  2.  
  3.     [SWF(width=1000,height=1000)]
  4.     import flash.display.*;
  5.     import flash.events.*;
  6.     public class RandomWalkTexture extends Sprite {
  7.  
  8.         private var _canvas:BitmapData;
  9.         private var _populationNum:int=100;
  10.         private var _movers:Vector.<Mover>;
  11.         public function RandomWalkTexture() {
  12.             var sw:Number=stage.stageWidth;
  13.             var sh:Number=stage.stageHeight;
  14.             scaleX=scaleY=.25;
  15.             _canvas=new BitmapData(sw*4,sh*4,false,0x000000);
  16.             addChild(new Bitmap(_canvas));
  17.            
  18.             _movers = new Vector.<Mover>();
  19.             for (var i:int = 0; i<_populationNum; i++) {
  20.                 _movers[i]=new Mover(_canvas,sw*1.5+Math.random()*sw,sh*1.5+Math.random()*sh);
  21.             }
  22.             addEventListener(Event.ENTER_FRAME, onRun);
  23.         }
  24.         private function onRun(evt:Event):void {
  25.             for (var i:int = 0; i<200; i++) {
  26.                 for (var j:int = 0; j<_populationNum; j++) {
  27.                     _movers[j].run();
  28.                 }
  29.             }
  30.         }
  31.     }
  32. }
  33.  
  34. import flash.display.BitmapData;
  35. class Mover {
  36.     public var x:Number;
  37.     public var y:Number;
  38.     public var velX:Number;
  39.     public var velY:Number;
  40.     public var speed:Number;
  41.     private var _canvas:BitmapData;
  42.  
  43.     public function Mover(canvas:BitmapData, xp:Number, yp:Number) {
  44.         _canvas=canvas;
  45.         x=xp;
  46.         y=yp;
  47.         velX=0;
  48.         velY=0;
  49.         speed=Math.random()*5-2.5;
  50.     }
  51.     public function run():void {
  52.         x+=velX;
  53.         y+=velY;
  54.         _canvas.setPixel(x, y, 0xFFFFFF);
  55.         var dir:Number=int(Math.random()*4);
  56.         if (dir==0) {
  57.             velX=0;
  58.             velY=- speed;
  59.         } else if (dir == 1) {
  60.             velX=0;
  61.             velY=speed;
  62.         } else if (dir == 2) {
  63.             velX=- speed;
  64.             velY=0;
  65.         } else if (dir == 3) {
  66.             velX=speed;
  67.             velY=0;
  68.         }
  69.     }
  70. }

This snippet is meant to be run as a document class. Nothing special here... this is just something I found laying around - the actual bitmap being drawn is rather big, so I recommending right clicking (control clicking on mac) on the swf and to zoom in and out.

Here are a few images:




This entry was posted in misc and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

5 Comments

  1. David Marr
    Posted May 7, 2009 at 11:02 am | Permalink

    Hey this looks like a really cool example. I am trying to run it in Flex Builder in an AS3 project but I get an error here:

    1046: Type was not found or was not a compile-time constant: Vector. localhost/src RandomWalkTexture.as line 10 1241719222263 60

    Any idea how to resolve that?

    Thx,
    Dave

  2. David Marr
    Posted May 7, 2009 at 11:07 am | Permalink

    UPDATE: I realized you said it is meant to be a DocumentClass in the Flash IDE, so I tried that approach and it worked.

    Very cool script! I think it would make some nice backgrounds for other uses.

    I wonder why this wouldn’t work in Flex Builder though.. hmm

  3. Posted May 7, 2009 at 11:55 am | Permalink

    Hey David,

    The reason it didn’t work in your flex builder is because the Vector class exists only in flash 10… so you’ll need to setup your flex with fp10 in order to get it to work….

    cheers,

    z

  4. Andrew
    Posted November 11, 2009 at 1:50 pm | Permalink

    how can i use this? I’ve tried saving it as a graphic.as file and opening it through another fla where the document class set to graphic.. i get a weird syntax error with square triangle and arrow symbols as the source. Can anyone explain how to do this please?

  5. Posted November 11, 2009 at 2:12 pm | Permalink

    Hey Andrew… you just need to save it as “RandomWalkTexture.as” and use it as the doc class and you should be good to go.. make sure to enter:
    RandomWalkClass

    and not

    RandomWalkClass.as

    into the field for the document class inside of flash

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*