forked from: forked from: Smooth Green Dynamics Spheres
// forked from Carmon's forked from: Smooth Green Dynamics Spheres
// forked from jbraccini's Smooth Green Dynamics Spheres
package {
import flash.display.GradientType;
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.filters.GlowFilter;
import flash.geom.Matrix;
import flash.utils.Timer;
import flash.display.BlendMode;
[SWF(backgroundColor="#000000", frameRate=25)]
public class GradientSphere extends Sprite {
private const AMOUNT:Number = 50;
private var _x:Number = stage.stageWidth/2;
private var _y:Number = stage.stageHeight/2;
private var _power:Number = 25;
private var _epsilonSq:Number = 40000;
private var _gavityConst:Number = 10000;
private var lx:Number;
private var ly:Number;
private var ldSq:Number;
private var ld:Number;
private var lfactor:Number;
private var _dpoList:Array = [];
private var _colors:Array = [0xeeffee, 0xaaffaa, 0x22ff22, 0x00ff00];
private var _render:Timer;
private var _sequencer:Timer;
private var _background:Sprite;
public function GradientSphere() {
// write as3 code here..
init();
}
private function init():void {
_render = new Timer(33);
_render.addEventListener(TimerEvent.TIMER, handleRender);
_sequencer = new Timer(33);
_sequencer.addEventListener(TimerEvent.TIMER, handleSequence);
drawBackground();
start();
}
private function drawBackground():void {
var matrix:Matrix = new Matrix();
var backgroundSize:Number = 750;
matrix.createGradientBox(backgroundSize, backgroundSize, (3 * Math.PI / 2), 0, 10);
_background = new Sprite();
_background.graphics.beginGradientFill(GradientType.RADIAL, [0xfffffc, 0xa4ce2c, 0x1d9300, 0x003800], [100, 100, 100, 100], [0, 50, 100, 200], matrix);
_background.graphics.drawRect(0, 0, backgroundSize, backgroundSize);
_background.x = (-_background.width/2) + (stage.stageWidth/2);
_background.y = (-_background.height/2) + (stage.stageHeight/2);
addChild(_background);
}
private function start():void {
_render.start();
_sequencer.start();
}
private function stop():void {
_render.stop();
_sequencer.stop();
}
private function getNewGradientSphere(r:Number):Sprite {
var sphere:Sprite = new Sprite();
var color:uint = _colors[randRange(0, _colors.length)];
sphere.graphics.beginGradientFill(GradientType.RADIAL, [color, color], [1,0], [0,r]);
sphere.graphics.drawCircle(0,0,r);
sphere.graphics.endFill();
return sphere;
}
private function handleRender(e:TimerEvent):void {
var dpo:Object;
var sphere:Sprite;
for(var i:Number = 0; i < _dpoList.length; i++){
dpo = _dpoList[i];
sphere = dpo.sphere;
sphere.y += dpo.velY*10;
sphere.x += dpo.velX*10;
_dpoList[i] = calculateDynamics(dpo);
if(sphere.y > stage.stageHeight){
removeChild(sphere);
_dpoList.splice(i, 1);
}
if(sphere.y < 0){
removeChild(sphere);
_dpoList.splice(i, 1);
}
if(sphere.x > stage.stageWidth){
removeChild(sphere);
_dpoList.splice(i, 1);
}
if(sphere.x < 0){
removeChild(sphere);
_dpoList.splice(i, 1);
}
}
}
private function calculateDynamics(dpo:Object):Object{
lx = _x - dpo.sphere.x;
ly = _y - dpo.sphere.y;
ldSq = lx * lx + ly * ly;
var time:int = 25;
ld = Math.sqrt( ldSq );
if( ldSq < _epsilonSq ) ldSq = _epsilonSq;
lfactor = ( _power * time ) / ( ldSq * ld );
dpo.velX += lx * lfactor;
dpo.velY += ly * lfactor;
trace(lx*lfactor);
return dpo;
}
private function handleSequence(e:TimerEvent):void {
if(_dpoList.length < AMOUNT){
var dpo:Object = new Object();
var radius:Number = randRange(1, 50);
var sphere:Sprite = getNewGradientSphere(radius);
sphere.x = randRange(0, stage.stageWidth);
sphere.y = randRange(0, stage.stageHeight);
sphere.blendMode = BlendMode.ADD;
sphere.filters = [new GlowFilter(0x00ff00, 1, 15,15)];
dpo.sphere = sphere;
dpo.velX = 0;
dpo.velY = 0;
addChild(sphere);
_dpoList.push(dpo);
}
}
private function randRange(start:Number, end:Number) : Number {
return Math.floor(start +(Math.random() * (end - start)));
}
}
}