# BitmapData Timer

**URL:** <https://forum.kirupa.com/t/bitmapdata-timer/253959>\
**Category:** flash\
**Created:** [March 6, 2008, 8:57pm UTC](https://forum.kirupa.com/t/bitmapdata-timer/253959 "2008-03-06T20:57:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![scottse14](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@scottse14](https://forum.kirupa.com/u/scottse14)\
**Post date:** [March 6, 2008, 8:57pm UTC](https://forum.kirupa.com/t/bitmapdata-timer/253959/1 "2008-03-06T20:57:37Z")

</div>

Hi guys,  
I’m very new to flash (and to this forum) and I’m trying to create this timer for a school project. It’s basically going to be a pixel stopwatch that displays a new pixel every second using the BitmapData class. I looked at a few tutorials online to piece this together. It all works fine except for when I try and get all the pixels to clear after 1 minute and restart the function. I know what I’m doing wrong is probably something very basic, but will someone please point me in the right direction?  
Thanks for any help  
Scott

```auto
var photoBitmap:Bitmap; // Bitmap
var SecondsImgData:SecondsImg;	// SecondsImg bitmap (in library)
var bmpFx:Bitmap; // Bitmap Effect
var bmpFxData:BitmapData; // Bitmap Square
var increment:Number; // size of each square in pixels
var i:Number=0; // for pixel grid hotizontal increment
var j:Number=0; // for pixel grid vertical increment
var timer:Timer; // Timer

// Define Stage Variables
var stageW:Number = stage.stageWidth;
var stageH:Number = stage.stageHeight;

// Width (in pixels) of the drawn rectangle
	increment = 40;
	// creates an instance of SecondsImg bitmap data found in library
	SecondsImgData= new SecondsImg(0, 0);
	// creates a bitmapinstance of SecondsImgData
	photoBitmap = new Bitmap(SecondsImgData);
	// centers the counter
	photoBitmap.x = (stageW-photoBitmap.width)/2;
	photoBitmap.y = (stageH-photoBitmap.height)/2;
	photoBitmap.y = 0;
	photoBitmap.alpha = 0;
	// creates a bitmap
	bmpFxData = new BitmapData (photoBitmap.width,photoBitmap.height,false,0xFFFFFFFF);
	bmpFx = new Bitmap(bmpFxData);
	// place at the same position of existing bitmap
	bmpFx.x = photoBitmap.x;
	bmpFx.y = photoBitmap.y;
	
	// add the new bitmaps to the display list in following order
	addChild(bmpFx);
	addChild(photoBitmap);

	// Set new Timer 1 second loop infinite times
	timer = new Timer(1000,0)
	//start timer
	timer.start();
	// add listener to execute every 1 second
	timer.addEventListener(TimerEvent.TIMER,timerListener);

function timerListener(e:TimerEvent){
	// grabs the color from x,y pixel
	var color:uint = SecondsImgData.getPixel(i,j);
	// creates a new rectangle
	var square:Rectangle = new Rectangle(i,j,increment,increment);
	// fills the new rectangle with the color found in the "color" variable
	bmpFxData.fillRect(square,color);
	e.updateAfterEvent();
	j+= increment;
	if (j >= SecondsImgData.height) {i+=increment; j=0};
	if (i >= SecondsImgData.width) restart();

}

function restart(){
	i=0;
	j=0;
	SecondsImgData.dispose();
	bmpFxData.dispose();
	removeChild(bmpFx);
	removeChild(photoBitmap);
	timer.stop();
}
gotoAndPlay(1);

```
