LightningCrazy
@author Kaede[xingxx.com]
// write as3 code here..
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BitmapDataChannel;
import flash.display.BlendMode;
import flash.display.DisplayObject;
import flash.display.GradientType;
import flash.display.MovieClip;
import flash.display.SpreadMethod;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BlurFilter;
import flash.filters.ColorMatrixFilter;
import flash.geom.Matrix;
[SWF(backgroundColor="#000000")]
/**
* @author Kaede[xingxx.com]
*/
public class LightningCrazy extends MovieClip{
private var container:Sprite;
private var cover:Sprite;
private var blends:Array;
private var lightnings:Array;
private var n:Number = 5;
public function LightningCrazy():void {
addEventListener(Event.ADDED_TO_STAGE,init)
}
private function init(e:Event):void {
lightnings = [];
blends = [BlendMode.DARKEN,BlendMode.OVERLAY];
container = addChild(new Sprite()) as Sprite;
var base:Sprite;
for (var i:int = 0; i < n; i++) {
base = createBase();
lightnings.push(drawLightning(base));
}
cover = addChild(new Sprite()) as Sprite;
cover.blendMode = BlendMode.OVERLAY
drawGradient(cover,[0xFF00FF,0x0000FF])
addEventListener(Event.ENTER_FRAME, enterframeHandler)
}
private function enterframeHandler(e:Event):void {
container.filters = [new BlurFilter(Math.floor(Math.random()*15), Math.floor(Math.random()*15)),new ColorMatrixFilter()]
for (var i:int = 0; i < n; i++) {
var lightning:Bitmap = container.addChild(lightnings[Math.floor(Math.random() * lightnings.length)]) as Bitmap;
lightning.blendMode = blends[Math.floor(Math.random()*blends.length)]
lightning.y = Math.floor(Math.random()*stage.stageHeight)
}
//drawGradient(cover,[Math.random()*0xFFFFFF,Math.random()*0xFFFFFF])
}
private function drawLightning(base:Sprite):Bitmap {
var bmd:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight)
bmd.draw(base)
for (var traceHeight:int = 0; traceHeight < stage.stageHeight; traceHeight++) {
for (var traceWidth:int = 0; traceWidth < stage.stageWidth; traceWidth++) {
var colorCode:uint = bmd.getPixel(traceWidth, traceHeight);
if (colorCode <= 16400000){
bmd.setPixel(traceWidth,traceHeight,0)
}
}
}
return new Bitmap(bmd);
}
private function createBase():Sprite {
var base:Sprite = new Sprite()
var cloudBase:Sprite = base.addChild(new Sprite()) as Sprite;
var gradient:Sprite = cloudBase.addChild(new Sprite()) as Sprite;
var cloud = cloudBase.addChild(new Bitmap()) as Bitmap;
drawGradient(gradient,[0xFFFFFF,0x000000]);
drawCloud(cloud);
cloud.blendMode = BlendMode.DIFFERENCE
var invert:Sprite = base.addChild(new Sprite()) as Sprite;
drawInvert(invert)
return base
}
private function drawInvert(targetSprite:Sprite):void{
targetSprite.graphics.beginFill(0);
targetSprite.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
targetSprite.blendMode = BlendMode.INVERT
}
private function drawCloud(targetBitmap:Bitmap):void{
var bmd:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x00CCCCCC);
var seed:Number = Math.floor(Math.random() * 10);
var channels:uint = BitmapDataChannel.RED | BitmapDataChannel.BLUE | BitmapDataChannel.GREEN;
bmd.perlinNoise(100, 80, 6, seed, false, true, channels, true);
targetBitmap.bitmapData = bmd
}
private function drawGradient(targetSprite:Sprite,colors:Array):void{
var matr:Matrix = new Matrix();
matr.createGradientBox(stage.stageWidth, stage.stageHeight, 90 * Math.PI / 180, 0, 0);
targetSprite.graphics.beginGradientFill(GradientType.LINEAR, [colors[0], colors[1]], [1,1], [0,180],matr);
targetSprite.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
}
}
}