1
votes

I have a large sprite...and I'm splitting it into several smaller ones with function bellow. The function works great but the memory usage of my .swf grows dramatically.

What am I doing wrong?

private function split_sprite(sp:Sprite,the_parrent:Sprite) {


    var region:Rectangle = new Rectangle();
    region = sp.getBounds(the_parrent);

    var w=region.width/10;
    var h = region.height ;

    for (var i=0;i<10;i++){ 
            //Build Matrix Transform
            var mat:Matrix= new Matrix(1,0,0,1, 0-w*i, 0 );
            sp.transform.matrix = mat;

            //Draw the bitmap
        var temp:BitmapData = new BitmapData(w + 2 , h, true, 0x000000);


            temp.draw(sp,mat);

            //Attach the BitmapData to a Bitmap Instance
            var myBitmap:Bitmap = new Bitmap(temp);
            myBitmap.smoothing = true;
            //Re apply an Inverse Matrix
            mat = new Matrix(1,0,0,1, region.x, region.y);
            myBitmap.transform.matrix = mat;
            var spp=new Sprite();
             //spp.cacheAsBitmap=true;
            spp.addChild(myBitmap);
            spp.x=w*i;
            the_parrent.addChild(spp);


    }



}
1
Can you elaborate on the memory usage growing dramatically? Adding a bunch of bitmaps to the stage is going to increase the memory footprint... a leak would mean you are removing them but the memory isn't being released.Joel Hooks
It goes from using 30 MB to 100+ MB. Here's what I' trying to do : I'm working on a game that has pretty wide levels(9000+ px wide and 1500+ high), the terrain in the levels is generated via a list of points (vertices) and filled with a pattern with beginBitmapFill. The game works fine on my i5 processor but when I runn it on slower computer the frame rate drops from 27 fps to about 14-17fps. I did some digging and testing and found out that the large terrain is causing this problem.... so started reading on how to optimize it.George
I added a scrollRect to a parent sprite - this improved the performance a bit and I was reading about cache as bitmap ... but that has its limits ... so I decided to split my sprite so it's small enough to cache. Any suggestions on how to get this done? Or any other advice on improving performance. Thanks.George
Are you familiar with Bitmap Blitting? The basic concept is that you have your enormous 9000x1500 BitmapData object in memory somewhere and that's fine, but you never add it to stage. Instead, you add a single Bitmap to stage that fills the whole screen, then you use the BitmapData's "copyPixels()" method to populate your output bitmap on every frame.Myk
@Myk copyPixels()'s source is a bitmap data, so he need to draw the sprite onto a bitmap data firstlyHuang F. Lei

1 Answers

4
votes

If I remember correctly, BitmapData will never be garbage collected - you are creating a lot of new BMD objects, but when you are done with them you have to call myBitmapDataObject.dispose() to clean up after yourself. I dunno if that'll help - you don't have a lot of details on the nature of this memroy leak - but that's one place to look. BitmapData objects can eat up a lot of memory fast if you're not dispose()ing properly.