Les dejo esta clase pequeña que desarrolle, tvNoise
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.getTimer;
import flash.utils.Timer;
/**
* ...
* @author EzeQL
* @see www.EzeQL.com/blog
*/
public class NoiseTV extends Sprite {
private var _bitmap:Bitmap;
private var _bitmapData:BitmapData;
private var _bmpWidth:int;
private var _bmpheight:int;
private var _timer:Timer;
public function NoiseTV(pWidth:int=400,pHeight:int=300)
{
_bitmapData = new BitmapData(pWidth, pHeight, false, 0x000000);
_bitmap = new Bitmap(_bitmapData);
_bmpheight = pHeight;
_bmpWidth = pWidth;
}
public function start():void
{
addChild(_bitmap);
//Prueben Usar Ambos :=>
this.addEventListener(Event.ENTER_FRAME, NoiseTV_EnterFrame);
//_timer = new Timer(50);
//_timer.addEventListener(TimerEvent.TIMER, Timer_TimerEvent);
//_timer.start();
}
public function stop():void
{
this.removeEventListener(Event.ENTER_FRAME, NoiseTV_EnterFrame);
//_timer.stop();
}
private function drawNoise():void
{
var seed:int = int(Math.random() * int.MAX_VALUE);
//_bitmapData.lock();
_bitmapData.noise(seed, 0, 255, 0, true); // este hermoso metodo hace todo el trabajo por nosotros
//_bitmapData.unlock(); // para este uso no gana performance ..pruebenlo ustedes.
}
public function restart():void
{
stop();
removeChild(_bitmap);
_bitmapData = new BitmapData(_bmpWidth, _bmpheight, false, 0x000000);
_bitmap = new Bitmap(_bitmapData);
start();
}
private function NoiseTV_EnterFrame (event:Event):void
{
//var inicio:uint = getTimer();
drawNoise();
//trace(getTimer() - inicio);
}
private function Timer_TimerEvent(event:TimerEvent):void
{
//var inicio:uint = getTimer();
drawNoise();
//event.updateAfterEvent(); => en nuestro caso no tiene sentido
//trace(getTimer() - inicio);
}
public function get bmpheight():int { return _bmpheight; }
public function set bmpheight(value:int):void {
_bmpheight = value;
}
public function get bmpWidth():int { return _bmpWidth; }
public function set bmpWidth(value:int):void {
_bmpWidth = value;
}
}
}
clase Main,Ejemplo de uso
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
/**
* ...
* @author EzeQL
* @see www.EzeQL.com/blog
*/
public class Main extends Sprite{
private var noiseTv:NoiseTV;
public function Main() {
noiseTv = new NoiseTV();
addChild(noiseTv);
noiseTv.start();
stage.addEventListener(MouseEvent.CLICK, Stage_Click);
}
private function Stage_Click(e:MouseEvent):void
{
noiseTv.bmpheight = Math.random() * stage.stageHeight;
noiseTv.bmpWidth = Math.random() * stage.stageWidth
noiseTv.restart();
}
}
}