Hwang
Hwang

Reputation: 502

Drawing BitmapData to Sprite still using bitmapdata itself?

I'm calling images from library to be use. But because I will be constantly add and remove the images, so I tried to copy the bitmapdata to a sprite and reuse the sprite.

private function loadImage():void {
        for (var i:int = 1; i < typeAmount + 1; i++) {
            SlotClass = Main.queue.getLoader('main_uiMC').getClass('slot' + i + 'bmp') as Class;
            bmpDSlot = new SlotClass() as BitmapData;
            bmpDSlotV.push(bmpDSlot)
        }
    }

    private function bitmaping():void {
        for (var i:int = 1; i < typeAmount + 1; i++) {
            slotS = new Sprite()
            slotS.graphics.beginBitmapFill(bmpDSlotV[i - 1], new Matrix, false, true)
            slotS.graphics.drawRect(0, 0, bmpDSlotV[i - 1].width, bmpDSlotV[i - 1].height);
            slotS.graphics.endFill();
            bmpV.push(slotS)
        }

Every time I duplicate the sprite, flashdevelop's profiler showed that the bitmapdata is being added as well. Even when I use removeChild to remove the Sprite, the memory usage won't decrease. Is there a way to better copy the content of the bitmapdata, and can be completely remove when I remove the sprite?

*i will still be using the image, just that on that particular round i would like to remove the sprite that has the image.

Upvotes: 0

Views: 557

Answers (2)

Vesper
Vesper

Reputation: 18747

You should use a single BitmapData object, create numerous Bitmap objects and operate these.

 private function bitmaping():void {
    for (var i:int = 1; i < typeAmount + 1; i++) {
        slotS = new Bitmap(bmpDSlotV[i-1]);
        bmpV.push(slotS);
    }

Creating another Bitmap like this does not add another BitmapData, it uses existing one as reference.

Upvotes: 1

Andrey Popov
Andrey Popov

Reputation: 7510

Well your choice is to reuse the BitmapData. And because you need multiple instances of the same image, you need to create new one for each object that you would like to add. And this would be a Bitmap with the old BitmapData:

var originalBMD:BitmapData = instantiateItSomehow();

var first:Bitmap = new Bitmap(originalBMD);
var second:Bitmap = new Bitmap(originalBMD);

This way you would reuse the BitmapData and I think the memory will increase only because of the Bitmap objects, but the data itself should not be duplicated.

Upvotes: 0

Related Questions