forked from: lightning
/**
* Copyright okoi ( http://wonderfl.net/user/okoi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/uDMD
*/
// forked from kiyobu's lightning
package {
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.filters.GlowFilter;
import flash.filters.BlurFilter;
import flash.display.BlendMode;
import flash.geom.ColorTransform;
[SWF(width = "465", height = "465", backgroundColor = "0x000000", frameRate = "30")]
public class FlashTest extends Sprite {
public var layer:Array = new Array();
private static const LIGHT_LENGTH:int = 250;
private static const LIGHT_NUM:int = 40;
private var r:Number = 0;
public function FlashTest() {
layer[0] = new Bitmap(new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00000000));
layer[0].filters = [new BlurFilter(2,2,1)]
layer[1] = new Sprite();
layer[1].filters = [new BlurFilter(2,2,1), new GlowFilter(0x4411ff, 1, 32, 32, 8, 3)]
addChild(layer[0]);
addChild(layer[1]);
addEventListener(Event.ENTER_FRAME, loop);
}
public function loop(e:Event):void{
lightning(layer[1]);
var cash:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00000000);
cash.draw(this, null, new ColorTransform(1,1,1,0.9), BlendMode.ADD);
layer[0].bitmapData = cash;
r = (r + 1) % 360;
}
public function lightning(tg:Sprite):void{
var defX:Number = stage.stageWidth/2;
var defY:Number = stage.stageHeight/2;
var xx:Number = 0;
var yy:Number = 0;
var rx:Number = xx;
var ry:Number = yy;
var th:Number = random(2) +1
tg.graphics.clear();
for( var i:int = 0; i < LIGHT_NUM; i++ )
{
var angle:Number;
if( i % 2 == 0 ) angle = ((360 * i / LIGHT_NUM) + r) % 360;
else angle = ((360 * i / LIGHT_NUM) - r) % 360;
var radians:Number = angle * Math.PI/ 180;
var sin:Number = Math.sin( radians );
var cos:Number = Math.cos( radians );
xx = 0;
yy = 50;
rx = cos * xx - sin * yy;
ry = sin * xx + cos * yy;
tg.graphics.moveTo(rx+defX, ry+defY);
while(Math.abs(yy) < LIGHT_LENGTH){
xx += random(20) * (random(2)*2 -1);
yy += random(30) * 3;
rx = cos * xx - sin * yy;
ry = sin * xx + cos * yy;
tg.graphics.lineStyle(th,0xffffff,1);
tg.graphics.lineTo(rx+defX,ry+defY);
}
}
}
public function random(n:Number):Number{
return Math.floor(Math.random() * n);
}
}
}