LA Flash and Flex Developer
15 Jul
I only posted the AS 3.0 Make it Rain effect yesterday and I have been bombarded with people asking me to release the source. It’s not the tightest code yet but here it is anyway:
package {
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.geom.Matrix;
import flash.filters.ConvolutionFilter;
import flash.geom.ColorTransform;
import flash.filters.DisplacementMapFilter;
import flash.utils.Timer;
import flash.events.TimerEvent;
class RainMaker {
private var __scope:MovieClip;
private var __clip:MovieClip;
private var __sourceBitmap:BitmapData;
private var __bitmap1:BitmapData;
private var __bitmap2:BitmapData;
private var __buffer:BitmapData;
private var __outputImage:BitmapData;
private var __bounds:Rectangle;
private var __origin:Point;
private var __matrix:Matrix;
private var __matrix2:Matrix;
private var __wave:ConvolutionFilter;
private var __damp:ColorTransform;
private var __water:DisplacementMapFilter;
private var __surface:BitmapData;
private var __fillcolour:int = 128;
private var __xPos:int;
private var __yPos:int;
private var __lastx:int;
private var __lasty:int;
//Movieclips
private var cloneClip_mc:MovieClip;
function RainMaker(scope:MovieClip, clip:MovieClip) {
__scope = scope;
__clip = clip;
draw();
}
private function draw():void {
cloneClip_mc = new MovieClip();
__scope.addChild(cloneClip_mc);
var bitmapImage:BitmapData = new BitmapData(__clip.width, __clip.height, false, 0);
bitmapImage.draw(__clip);
__surface = bitmapImage;
__clip.visible = false;
__bitmap1 = new BitmapData(__clip.width, __clip.height, false, __fillcolour);
__bitmap2 = new BitmapData(__clip.width, __clip.height, false, __fillcolour);
__sourceBitmap = new BitmapData(__clip.width, __clip.height, false, __fillcolour);
__buffer = new BitmapData(__clip.width, __clip.height, false, __fillcolour);
__outputImage = new BitmapData(__clip.width, __clip.height, true, __fillcolour);
__bounds = new Rectangle(0, 0, __clip.width, __clip.height);
__origin = new Point();
__matrix = new Matrix();
__matrix2 = new Matrix();
__matrix2.a = __matrix2.d = 2;
__wave = new ConvolutionFilter(3, 3, [1, 1, 1, 1, 1, 1, 1, 1, 1], 9, 0);
__damp = new ColorTransform(0, 0, 0.99609374, 1, 0, 0, 2, 0);
__water = new DisplacementMapFilter(__bitmap2, __origin, 4, 4, 32, 32, "ignore");
var bm:Bitmap=new Bitmap(__outputImage);
cloneClip_mc.addChild(bm);
var __waterTimer:Timer = new Timer(10);
__waterTimer.addEventListener('timer', turnOnWater);
__waterTimer.start();
var myTimer:Timer = new Timer(150);
myTimer.addEventListener('timer', waterEffect);
myTimer.start();
}
private function fitsArea():Boolean {
var curX = __xPos;
var curY = __yPos;
if ((curX < __clip.width) && (curY < __clip.height)) {
return true;
} else {
return false;
}
}
private function waterEffect(ev:Object):void {
if (isNaN(__xPos)) {
__xPos = 0;
__yPos = 0;
} else {
if (fitsArea()) {
__xPos = Math.random()*__clip.width;
__yPos = Math.random()*__clip.height;
} else {
__xPos = 0;
__yPos = 0;
}
}
__lastx = __xPos;
__lasty = __yPos;
}
private function turnOnWater(ev:Object):void {
var curX = __xPos / 2;
var curY = __yPos / 2;
if ((__lasty != curY) || (__lastx != curX)) {
__sourceBitmap.setPixel(curX + 1, curY, 16777215);
__sourceBitmap.setPixel(curX - 1, curY, 16777215);
__sourceBitmap.setPixel(curX, curY + 1, 16777215);
__sourceBitmap.setPixel(curX, curY - 1, 16777215);
__sourceBitmap.setPixel(curX, curY, 16777215);
}
__lastx = curX;
__lasty = curY;
__bitmap1.applyFilter(__sourceBitmap, __bounds, __origin, __wave);
__bitmap1.draw(__bitmap1, __matrix, null, "add");
__bitmap1.draw(__buffer, __matrix, null, "difference");
__bitmap1.draw(__bitmap1, __matrix, __damp);
__bitmap2.draw(__bitmap1, __matrix2, null, null, null, true);
__outputImage.applyFilter(__surface, new Rectangle(0, 0, __clip.width,
__clip.height), __origin, __water);
__buffer = __sourceBitmap;
__sourceBitmap = __bitmap1.clone();
}
}
}