Tag Archive > Flash

RandomWalk

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

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>*/

Continue reading...

Tags: , , , ,

Homegrown Tentacles

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

I had been scurrying about over on the Soulwire blog, again, and looking at the tentacles. I did some experiments of
my own, but the tentacles never came out right, not organic enough. I wrote Justin and asked him how he did it and he was really cool about helping out with a code example of how he did that part.

The Soulwire Tentacles was an experiment in PixelBender speed and usability, so he makes it look as if the tentacles are really growing, i.e. starts out small and each “joint” grows as the tentacles grows. Mine is just a look into the random-organic way they grow. Also I was trying to see what kinds of speed I would get by using the Bitmap approach.

Here are the Soulwire Tentacles

And here are my Tentacles… that came out wrong… :)

The code:

/**
*
 * @BitmapSpore.as	:	inspired by http://blog.soulwire.co.uk/flash/actionscript-3/pixel-bender-growing-tentacles/
 * @Date  08/06-09
 * @Ricki Gregersen
*/

package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Point;

	[SWF( width="512", height = "512", backgroundColor = "0x222222", frameRate = "30" )]

	public class BitmapSpore extends Sprite
	{
		private static var fading : ColorTransform = new ColorTransform( 1, 1, 1, 0.99999 );
		private static var _blur : BlurFilter = new BlurFilter( 1, 1, 2 );

		private var _bmd:BitmapData;
		private var _bmp:Bitmap;
		private var _container:Sprite;

		public function BitmapSpore()
		{
			_bmd = new BitmapData( 512, 512, true, 0x222222 );
			_bmp = new Bitmap( _bmd );
			addChild( _bmp );
			var s:Spore;
			addChild( _container = new Sprite() );
			for( var i:int = 0; i < 500; i++ )
			{
				s = new Spore( 256, 256, range( 0.01, 0.05 ), range( 2.0, 3.0 ), range( 0.2, 1.0 ));
				s.scaleX = s.scaleY = Math.random() * 2;
				_container.addChild( s );
			}
			addEventListener( Event.ENTER_FRAME, update );
		}

		private function update( event:Event ):void
		{
			var n:int = _container.numChildren;
			var s:Spore;

			for( var i:int = 0; i < n; i++ )
			{
				s = Spore( _container.getChildAt( i ));
				s.move();
				s.x = s.loc.x;
				s.y = s.loc.y;
			}
			_bmp.filters = [  _blur ];
			_bmd.colorTransform( _bmd.rect, fading );
			_bmd.draw( _container );
		}

		private function range(min:Number, max:Number = NaN):Number
		{
			if(isNaN(max)) {max = min; min = 0}
				return min + (Math.random() * (max - min));
		} 

	}
}
	import flash.geom.Point;
	import flash.display.Shape;

	class Spore extends Shape
	{
		public var loc:Point;

		private var angStep:Number;
		private var posStep:Number;
		private var curl:Number;
		private var n:Number = 0;
		private var a:Number = Math.random() * Math.PI * 2;

		function Spore( xPos:Number, yPos:Number, aStep:Number, pStep:Number, c:Number )
		{
			loc = new Point( xPos, yPos );
			angStep = aStep;
			posStep = pStep;
			curl = c;
			graphics.lineStyle( 0, 0x222222, 0.7 );
			graphics.beginFill( 0xFF6600, 0.7 );
			graphics.drawCircle( 0, 0, 5 );
			graphics.endFill();
		}
		public function move():void
		{
			n += Math.random() * (angStep - -angStep) + -angStep;
			n *= 0.9 + curl * 0.1;
			a += n;
			loc.x += Math.cos( a ) * posStep;
			loc.y += Math.sin( a ) * posStep;
			this.scaleX = this.scaleY *= 0.98;
			var s:Number = this.scaleX = this.scaleY;
			if( s < 0.1 )
			{
				loc.x = loc.y = 256;
				this.scaleX = this.scaleY = Math.random() * 2;
			}
		}
	}

Continue reading...

Tags: , , , , , ,