RandomWalk

Ricki » 06 August 2009 » In ActionScript 3.0, ActionScript3, Flash, Particle »

Im looking into some of the random walk stuff out there and I stumbled upon www.actionsnippet.com and his RandomWalk experiments, great site by the way, bring coffee.
The thing that was cool about his way of doing it was that he constrains the “walkers” to only move: up, down, left and right. Absolutely no diagonal movement. This produces a strange grid looking pattern.

The code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*<pre lang="actionscript">*/
//playing around with something Zevan Rosser from http://actionsnippet.com/?p=1236 did
//on his blog

package {
   
    import flash.display.*;
    import flash.events.*;
   
    [SWF( width="512", height = "512", backgroundColor = "0x222222", frameRate = "120" )]
   
    public class RandomWalk extends Sprite
    {
        private var _canvas:BitmapData;
        private var _count:int = 100;
        private var _movers:Vector.<Mover>;
       
        public function RandomWalk()
        {
            var w:Number = 512;
            var h:Number = 512;
            scaleX=scaleY = 0.25;
            _canvas = new BitmapData( w * 4, h * 4, false, 0x222222 );
            addChild( new Bitmap( _canvas ));
           
            _movers = new Vector.<Mover>();
           
            for ( var i:int = 0; i < _count; i++ )
            {
                _movers[ i ] = new Mover( _canvas, w * 1.5 + Math.random() * w, h * 1.5 + Math.random() * h );
            }
            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 < _count; 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;
    private var _color:uint;
   
    public function Mover( canvas:BitmapData, xp:Number, yp:Number )
    {
        _color = 0xFF6600;//Math.random() * 0xFFFFFF;
        _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, _color );
       
        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;
        }
    }
}

/*</pre>*/

Tags: , , , ,

Trackback URL

No Comments on "RandomWalk"

Hi Stranger, leave a comment:

ALLOWED XHTML TAGS:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to Comments