PDA

View Full Version : How do I clear a BitmapData object?


rustyofco
07-02-2008, 08:57 PM
First I set up a bitmapdata object:

var bd:BitmapData = new BitmapData(500,500,true,0x00000000);
var bmp:Bitmap = new Bitmap(bd);
stage.addChild(bmp);

then, occasionally I will draw stuff in that bd object. So it's all good.
(I'm drawing things using the functions from this class:
http://www.bytearray.org/?p=67)
That code draws stuff into the bd object.

But once in a while I want to erase everything I drew, making a new bd object.
So I try this code... but it doesn't work:

function clearbd(){

var bd:BitmapData = new BitmapData(500,500,true,0x00000000);
contbg2.removeChild(bmp);
var bmp:Bitmap = new Bitmap(bd);
contbg2.addChild(bmp);
}

Basically, I redefine bd and bmp to new, blank BitmapData and Bitmap objects... But this doesn't work...

wvxvw
07-02-2008, 11:43 PM
1. The problem should be in some other place. If you remove bitmap from display list it shouldn't be visible.
2. I'd use BitmapData.dispose() method anyway before removing old Bitmapdata (so it won't stay in memory for ever =).

Silver55
07-03-2008, 01:44 PM
hi, ...why also a new Bitmap ????... 1 BitMap and 2 BitmapData..(or 3 to work.. ). After to draw or clear, reassigned it to the Bitmap...


// .................................................. .......
// ...How do I clear a BitmapData object?
// .................................................. .......

// ...my BlackBoard
var bd:BitmapData = new BitmapData(300,200, true, 0x33333333);
var bmp:Bitmap = new Bitmap(bd);
bmp.x = 20;
bmp.y = 20;
stage.addChild(bmp);

// ...2 buttons on the stage ( for example )
btn_draw.addEventListener(MouseEvent.CLICK, drawBlackBoard);
btn_clear.addEventListener(MouseEvent.CLICK, clearBlackBoard);


// .................................................. .......
// FUNCTION : drawBlackboard()
// draw something on the BlackBoard
// .................................................. .......
function drawBlackBoard(e:MouseEvent):void
{
// bd is a new objct...(local) we use to draw
var bd:BitmapData = new BitmapData(300,200,true,0x33333333);
var i :uint;

// ...stuff new bd ( ...a red line )
for (i = 10; i < 200; i++)
{
var red:uint = 0x60FF0000;
bd.setPixel32(i, 40, red);
}
// stuff ... stuff...

// now assign the (new) bitmapData to bmp (BitMap)
bmp.bitmapData = bd;
}
// .................................................. .......


// .................................................. .......
// FUNCTION : clearBlackboard()
// clear all
// .................................................. .......
function clearBlackBoard(e:MouseEvent):void
{
// ...new object (local), we use to clear :)
var bd:BitmapData = new BitmapData(300,200,true,0x33333333);

// assign the (new) bitmapData to bmp (BitMap)
bmp.bitmapData = bd;
}
// .................................................. .......

// :) :)