Sentinel
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.GradientType;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.filters.BlurFilter;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.utils.Timer;
[SWF(width="500",height="500",backgroundColor="#660000", frameRate="25")]
public class BitmapParticles extends Sprite {
private var _particles:Array;
private var _particles_max:Number = 1500;
private var _particles_holder:Sprite;
private var _bmp:Bitmap;
private var _bmd:BitmapData;
private var _render:Timer;
private var _transform:ColorTransform;
private var _mouseDown:Boolean;
private var _focusLayer:Sprite;
public function BitmapParticles(){
super();
init();
initParticles(_particles_max);
placeParticles();
placeFocusLayer();
start();
}
private function init():void {
trace("BitmapParticles::init");
_render = new Timer(33);
_render.addEventListener(TimerEvent.TIMER, renderParticles);
_transform = new ColorTransform(1, 1, 1, 0.95);
_bmd = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x660000);
_bmp = new Bitmap(_bmd);
_bmp.filters = [new BlurFilter()];
addChild(_bmp);
_particles_holder = new Sprite();
addChild(_particles_holder);
_particles = new Array();
stage.addEventListener(MouseEvent.CLICK, handleMouseDown);
}
private function initParticles(max:Number):void {
trace("BitmapParticles::initParticles");
var newParticle:Particle;
for(var i:Number = 0; i < max; i++){
newParticle = new Particle(randRange(1000, 5000), randRange(1, 3));
newParticle.x = randRange(10, stage.stageWidth -10);
newParticle.y = randRange(10, stage.stageHeight -10);
newParticle.r = randRange(0, 360);
_particles.push(newParticle);
}
}
private function placeParticles():void {
trace("BitmapParticles::placeParticles");
for(var i:Number = 0; i < _particles.length; i++){
_particles_holder.addChild(_particles[i]);
}
}
private function start():void {
_render.start();
}
private function renderParticles(e:TimerEvent):void {
var particle:Particle;
var degrees:Number;
for(var i:Number = 0; i < _particles.length; i++){
particle = _particles[i];
particle.seed += randRange(.01, 1);
particle.r += Math.cos(particle.seed * Math.PI / 180);
particle.xvel = particle.mobility * Math.cos(particle.r * Math.PI / 180 );
particle.yvel = particle.mobility * Math.sin(particle.r * Math.PI / 180 );
particle.x += particle.xvel;
particle.y += particle.yvel;
/* if(_mouseDown){
particle.x += (mouseX - particle.x) / 50;
particle.y += (mouseY - particle.y) / 50;
} */
if(hitBounds(particle)){
particle.r += 45;
}
}
_bmp.bitmapData.colorTransform(new Rectangle(0, 0, stage.stageWidth, stage.stageHeight), _transform);
_bmp.bitmapData.draw(_particles_holder);
}
private function hitBounds(particle:Particle):Boolean {
var result:Boolean = false;
if(particle.x < 0 || particle.x > stage.stageWidth){
result = true;
}
if(particle.y < 0 || particle.y > stage.stageHeight){
result = true;
}
return result;
}
private function placeFocusLayer():void {
var matrix:Matrix = new Matrix();
var backgroundSize:Number = 500;
matrix.createGradientBox(backgroundSize, backgroundSize, (3 * Math.PI / 2), 0, 0);
_focusLayer = new Sprite();
_focusLayer.graphics.beginGradientFill(GradientType.RADIAL, [0x000000, 0x000000], [0, 100], [0, 255], matrix);
_focusLayer.graphics.drawRect(0, 0, backgroundSize, backgroundSize);
_focusLayer.x = (-_focusLayer.width/2) + (stage.stageWidth/2);
_focusLayer.y = (-_focusLayer.height/2) + (stage.stageHeight/2);
_focusLayer.blendMode = "multiply";
addChild(_focusLayer);
}
private function handleMouseDown(e:MouseEvent):void {
trace("BitmapParticles::mouseDown");
for(var i:Number = 0; i < _particles.length; i++){
_particles[i].r = -_particles[i].r;
}
}
private function randRange(start:Number, end:Number) : Number {
return Math.floor(start +(Math.random() * (end - start)));
}
}
}
import flash.display.Sprite;
class Particle extends Sprite {
private var _life:Number;
private var _mobility:Number;
private var _center:Number;
private var _r:Number = Math.random() * 360;
private var _xvel:Number = 0;
private var _yvel:Number = 0;
private var _seed:Number = 0;
public function Particle(life:Number, mobility:Number = 0.001) {
super();
_life = life;
_mobility = mobility;
init();
}
private function init():void {
draw();
}
private function draw():void {
this.graphics.beginFill(Math.random() * 0xFFFFFF << 16);
this.graphics.drawCircle(0,0,1);
}
public function set xvel(v:Number):void { _xvel = v; }
public function set yvel(v:Number):void { _yvel = v; }
public function set seed(v:Number):void { _seed = v; }
public function set center(v:Number):void { _center = v; }
public function set r(v:Number):void { _r = v; }
public function get xvel():Number { return _xvel; }
public function get yvel():Number { return _yvel; }
public function get mobility():Number { return _mobility; }
public function get life():Number { return _life; }
public function get r():Number { return _r; }
public function get center():Number { return _center; }
public function get seed():Number { return _seed; }
}