Actionscript:
-
package {
-
-
[SWF(width=1000,height=1000)]
-
import flash.display.*;
-
import flash.events.*;
-
public class RandomWalkTexture extends Sprite {
-
-
private var _canvas:BitmapData;
-
private var _populationNum:int=100;
-
private var _movers:Vector.<Mover>;
-
public function RandomWalkTexture() {
-
var sw:Number=stage.stageWidth;
-
var sh:Number=stage.stageHeight;
-
scaleX=scaleY=.25;
-
_canvas=new BitmapData(sw*4,sh*4,false,0x000000);
-
addChild(new Bitmap(_canvas));
-
-
_movers = new Vector.<Mover>();
-
for (var i:int = 0; i<_populationNum; i++) {
-
_movers[i]=new Mover(_canvas,sw*1.5+Math.random()*sw,sh*1.5+Math.random()*sh);
-
}
-
addEventListener(Event.ENTER_FRAME, onRun);
-
}
-
private function onRun(evt:Event):void {
-
for (var i:int = 0; i<200; i++) {
-
for (var j:int = 0; j<_populationNum; j++) {
-
_movers[j].run();
-
}
-
}
-
}
-
}
-
}
-
-
import flash.display.BitmapData;
-
class Mover {
-
public var x:Number;
-
public var y:Number;
-
public var velX:Number;
-
public var velY:Number;
-
public var speed:Number;
-
private var _canvas:BitmapData;
-
-
public function Mover(canvas:BitmapData, xp:Number, yp:Number) {
-
_canvas=canvas;
-
x=xp;
-
y=yp;
-
velX=0;
-
velY=0;
-
speed=Math.random()*5-2.5;
-
}
-
public function run():void {
-
x+=velX;
-
y+=velY;
-
_canvas.setPixel(x, y, 0xFFFFFF);
-
var dir:Number=int(Math.random()*4);
-
if (dir==0) {
-
velX=0;
-
velY=- speed;
-
} else if (dir == 1) {
-
velX=0;
-
velY=speed;
-
} else if (dir == 2) {
-
velX=- speed;
-
velY=0;
-
} else if (dir == 3) {
-
velX=speed;
-
velY=0;
-
}
-
}
-
}
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:
5 Comments
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
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
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
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?
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