forked from: forked from: Smooth Green Dynamics Spheres
// forked from hacker_r1ekf_by'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 = 300;
private const GRAVITY:Number = .5;
private var _wind:Number = 0;
private var _windSeed:Number = 0;
private var _dpoList:Array = [];
private var _colors:Array = [0xfffffc, 0xe3ff00, 0xa2ff00, 0xffd5f2];
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);
return sphere;
}
private function handleRender(e:TimerEvent):void {
var dpo:Object;
var sphere:Sprite;
var speed:Number;
_wind = Math.sin(_windSeed++);
_wind -= GRAVITY;
for(var i:Number = 0; i < _dpoList.length; i++){
dpo = _dpoList[i];
sphere = dpo.sphere;
speed = dpo.speed;
sphere.y += speed;
sphere.x += dpo.wind;
_dpoList[i] = calculateDynamics(dpo)
if(sphere.y > stage.stageHeight + 200){
removeChild(sphere);
_dpoList.splice(i, 1);
}
}
}
private function calculateDynamics(dpo:Object):Object {
dpo.wind += _wind;
return dpo;
}
private function handleSequence(e:TimerEvent):void {
if(_dpoList.length < AMOUNT){
var dpo:Object = new Object();
var speed:Number = randRange(5, 20);
var radius:Number = randRange(6, 50);
var sphere:Sprite = getNewGradientSphere(radius);
sphere.x = stage.stageWidth;
sphere.y = -200;
sphere.blendMode = BlendMode.ADD;
sphere.filters = [new GlowFilter(0xe8ff07, 1, 15,15)];
dpo.sphere = sphere;
dpo.speed = speed;
dpo.wind = 0;
addChild(sphere);
_dpoList.push(dpo);
}
}
private function randRange(start:Number, end:Number) : Number {
return Math.floor(start +(Math.random() * (end - start)));
}
}
}