Search code examples
actionscript-3memory-leaksbitmapdata

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.


Solution

  • 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.