package {
//phlashers year end AS3 contest 2009
//swarm follows pointer
//http://www.phlashers.com
//hold down mouse button = ultraelectromagnetic storm
import flash.display.*
import flash.events.*
import flash.geom.*
public class SG extends MovieClip{
public var bd:BitmapData = new BitmapData(500,480,true,0x00000000)
public var bd2:BitmapData = new BitmapData(500,480,true,0xFF000000)
public var bmp:Bitmap = new Bitmap(bd)
public var bmp2:Bitmap = new Bitmap(bd2)
public var m:Matrix = new Matrix()
public var pA:Array = new Array()
public var sS:int = 200
public var swm:BitmapData = new BitmapData(3,3)
public var mkr:Sprite = new Sprite()
public static var rpl:Boolean = false
public static var bP:Number = .001
public function SG() {
addEventListener(Event.ENTER_FRAME,rtn,false,0,true)
stage.addEventListener(MouseEvent.MOUSE_DOWN,rplSwm,false,0,true)
stage.addEventListener(MouseEvent.MOUSE_UP,attSwm,false,0,true)
addChild(bmp2)
addChild(bmp)
for (var i:int =0; i<sS;i++) {
aSW(mkr.x,mkr.y,mkr).life = -1
}
}
public function rtn(e:Event):void {
var cTrn:ColorTransform = new ColorTransform()
cTrn.alphaMultiplier = .9
bd.lock()
bd2.lock()
bd.colorTransform(bd.rect,cTrn)
prtn()
mkr.x = mouseX
mkr.y = mouseY
bd.unlock()
bd2.unlock()
}
protected function prtn():void {
for (var i:String in pA) {
var p:SW = pA[i]
p.rtn()
if (p.x > 500) {
p.x=500
}else if (p.x <0) {
p.x=0
}
if (p.y > 480) {
p.y=480
}else if (p.y <0) {
p.y=0
}
p.tP.x = mkr.x
p.tP.y= mkr.y
if (Math.random() < bP) {
p.r = 20+ Math.random()*100
p.g = 30+ Math.random()*100
p.b = 150 + Math.random()*100
var s:SW = pA[Math.floor(Math.random() * pA.length)]
var b:Sprite = sB(s.x,s.y,p.x,p.y,-s.col)
m.tx = s.x
m.ty = s.y
bd.draw(b,m)
}
var pt:Point = new Point(p.x-2,p.y-2)
var cTrn:ColorTransform = new ColorTransform()
cTrn.color = p.col
swm.colorTransform(swm.rect, cTrn)
bd.copyPixels(this[p.img],this[p.img].rect,pt,null,null,true)
}
}
protected function aSW(xC:Number = 0,yC:Number = 0,tObj:Object = null):SW {
var p:SW = new SW()
pA.push(p)
p.x = xC
p.y = yC
p.tObj = tObj
return p
}
public function rplSwm(e:Event):void {
bP = .1
SG.rpl = true
}
public function attSwm(e:Event):void {
bP = .001
SG.rpl = false
}
protected function sB (xC1:Number,yC1:Number,xC2:Number,yC2:Number,col:uint=0xFF0000):Sprite {
var freq:Number=20;
var amp:Number=10;
var dur:uint=1;
var fx:Sprite=new Sprite ();
fx.x=xC1;
fx.y=yC1;
var xd:Number=xC2 - xC1;
var yd:Number=yC2 - yC1;
var x1:Number =0;
var y1:Number =0
var x2:Number =xd;
var y2:Number =yd
var xseg:Number=Math.round(xd / freq);
var yseg:Number=Math.round(yd / freq);
var ref:Object = fx.graphics;
ref.moveTo (x1,y1);
ref.lineStyle (1,col,1);
var i:Number=0;
for (i=1; i < freq; i++) {
var xpt:Number=x1 + i * xseg + Math.random() * amp - amp / 2;
var ypt:Number=y1 + i * yseg + Math.random() * amp - amp / 2;
ref.lineTo (xpt,ypt);
}
ref.lineTo (x2,y2);
return fx
}
}
}
import flash.geom.Point
dynamic class SW{
public var rR:Number = 20
public var sp:Number = 5
public var a:Number = 255
public var r:Number = 255
public var g:Number = 255
public var b:Number = 255
public var r2:Number = 255
public var g2:Number = 0
public var b2:Number = 0
public var x:Number = 0
public var y:Number = 0
public var rot:Number = 0
public var col:uint = 0x000000;
public var xS:Number = Math.cos(rot * (Math.PI/180))
public var yS:Number = Math.sin(rot * (Math.PI/180))
public var split:Number = 100
public var tR:Number = 0
public var tP:Point
public var life:Number = 100
public var jnk:Boolean = false
public var img:String = "swm"
public function SW() {
rot = Math.random() * 360
tP = new Point(0,0)
img = "swm"
r = 0
g = 0
b = 0
}
public function aCl():void {
col = (r << 16 | g << 8 | b)
}
public function rtn():void {
var dx:Number = tP.x -x
var dy:Number =tP.y -y
var d:Number = Math.abs(dx) + Math.abs(dy)
var a:Number = Math.atan2(dy,dx)
r -= (r-r2)/20
g -= (g-g2)/20
b -= (b-b2)/20
if (!SG.rpl) {
tR = a * (180/Math.PI)
}else{
tR = (a * (180/Math.PI)) + 180
}
if (tR - rot > 180) {
tR -= 360;
}else if (rot - tR > 180){
tR += 360;
}
if (rot-tR == 0) {
}else{
rot -= (Math.abs(rot - tR)/(rot - tR)) * (Math.random() * rR);
}
xS = sp * Math.cos(rot * (Math.PI/180))
yS = sp * Math.sin(rot * (Math.PI/180))
x+=xS
y+=yS
b += d/10
if (b > 255) {
b = 255
}
aCl()
}
}