So, I was following this tutorial from activetuts+ to create the same effect, a static distortion effect using the displacement map filter, in one of my game projects and it was cool until I realized that my project is getting fatter and fatter by adding images of different sizes to use for different sized objects.
Ofcourse, this was not acceptable so I thought I could create a random generated image by code and reuse it than to create different images in Photoshop.
Anyway, here’s the code which is generating a random image to be used with the effect in the tutorial.
function rndImage(w:Number, h:Number, wl:int = 0, bl:int =0):BitmapData { var bitmapdata:BitmapData = new BitmapData(w,h,false, 0xDDDDDD); var randY:int = 0; var randX:int = 0; var randW:int = 0; var thick:Number = 0; var a:int = 0; var minWidth:int = 0; var line:Shape = new Shape(); for ( a = 0; a< wl; a++) { thick = Math.random()* 1.3 + 0.2; randY = Math.random() * h; minWidth = (40 * w)/100; randW = Math.random() * (w-minWidth) + minWidth; randX = Math.random() * ((w-randW)); line.graphics.lineStyle(thick, 0xFFFFFF); line.graphics.moveTo(randX,randY); line.graphics.lineTo(randW, randY); } for ( a = 0; a< bl; a++) { thick = Math.random()* 1.3 + 0.2; randY = Math.random() * h; minWidth = (40 * w)/100; randW = Math.random() * (w-minWidth) + minWidth; randX = Math.random() * ((w-randW)); line.graphics.lineStyle(thick, 0x000000); line.graphics.moveTo(randX,randY); line.graphics.lineTo(randW+randX, randY); } bitmapdata.draw(line); return bitmapdata; }
And this is how you generate the image
rndImage(width,height, nr_white_lines,nr_dark_lines);