Archive > August 2009

Growing Homegrown Tentacles

Ricki » 08 August 2009 » In ActionScript 3.0, ActionScript3, Flash » 4 Comments

Well, I just got time to look into how to get the tentacles growing, i.e. start out small and have all the joints gradually increase in size, instead of the joint getting smaller all the time. (think I just tried to explain the definition of growing by using the definition of growing??).

Thanks again to Justin.

The spore Class turned out to look like this, it needs going over, but I was thinking on porting it to Processing and really get my tentacles on :)

The code:

package
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.utils.setTimeout;

	public class Spore extends Sprite
	{
		private static const LENGTH:int = 90;
		private var _posX:Vector.;
		private var _posY:Vector.;
		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;
		private var _index:int;
		private var _g:Graphics;

		public function Spore()
		{
			_posX = new Vector.( LENGTH, true );
			_posY = new Vector.( LENGTH, true );
			_g = graphics;
			this.generate( range( 0.01, 0.05 ), range( 2.0, 3.0 ), range( 0.2, 1.0 ) );
		}

		public function generate( aStep:Number, pStep:Number, c:Number ):void
		{
			_index = 0;
			angStep = aStep;
			posStep = pStep;
			curl = c;
			var loc:Point = new Point( 0, 0 );
			while( _index < LENGTH )
			{
				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;
				_posX[ _index ] = loc.x;
				_posY[ _index ] = loc.y;
				_index++;
			}
			_index = 0;
		}
		public function step():void
		{
			_g.clear();
			_g.lineStyle( 0, 0x000000, 0.7 );
			var c:int = 0;
			var radius:Number;
			while( c < _index )
			{
				radius = _index / c;
				if( radius > 10 ) radius = 10;
				_g.beginFill( 0xFF6600, 0.4 );
				_g.drawCircle( _posX[ c ], _posY[ c ], radius );
				_g.endFill();
				c++;
			}
			_index++;
			if( _index >= LENGTH )
			{
				this.generate( range( 0.01, 0.05 ), range( 2.0, 3.0 ), range( 0.2, 1.0 ) );
			}
		}
		private function range(min:Number, max:Number = NaN):Number
		{
			if(isNaN(max)) {max = min; min = 0}
				return min + (Math.random() * (max - min));
		}
	}
}

Continue reading...

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: , , , ,