Worm Study01
Study for color generation.
// forked from fladdict's code on 2008-12-17
// write as3 code here..
// Study for color generation.
package{
import flash.display.*
import flash.events.*
import flash.geom.*
public class Sketch02 extends Sprite
{
public var bm:Bitmap;
public var bmd:BitmapData;
public var particles:Array;
public var numParticles:int = 100;
public function Sketch02():void
{
bmd = new BitmapData(stage.stageWidth,stage.stageHeight, false, 0x000000);
bm = new Bitmap(bmd);
addChild(bm);
particles = [];
for(var i:int=0; i<numParticles; i++)
{
var prt:Object = createParticle();
particles.push(prt);
}
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
public function enterFrameHandler(e:Event):void
{
var n:int = particles.length;
for(var i:int=0; i<n; i++)
{
var prt:Object = particles[i];
prt.x += Math.cos(prt.direction) * prt.v;
prt.y += Math.sin(prt.direction) * prt.v;
if(prt.x < 0 ){
prt.x = stage.stageWidth;
}else if(prt.x > stage.stageWidth){
prt.x = 0;
}
if(prt.y < 0 ){
prt.y = stage.stageHeight;
}else if(prt.y > stage.stageHeight){
prt.y = 0;
}
bmd.setPixel(prt.x, prt.y, prt.color);
}
}
protected function createParticle():Object
{
var obj:Object = {};
obj.x = stage.stageWidth * 0.5;
obj.y = stage.stageHeight * 0.5;
obj.v = 1 + Math.random();
obj.direction = Math.random()*Math.PI*2;
obj.color = getColor();
return obj;
}
protected function getColor():int
{
var r:int = Math.random()*255;
var g:int = Math.random()*r;
var b:int = Math.random()*g;
return r<<16 | g<<16 | b;
}
protected function normRand():Number
{
return (Math.random()+Math.random()+Math.random()+Math.random()+Math.random())/5 - 0.5;
}
}
}