Search code examples
actionscript-3classloaderremovechild

as3 removeChild and Loaders in them?


I am creating multiple loaders to load images with a for loop. I am then placing those images in a new MoveClip.

When needed I am going to removeChild(flagHolder); Which holds all the images of flags loaded in the loaders. I am wondering will this delete all the myLoaders loaded into the parent movie clip I am removing?

I am hoping so that it may not some how take up resources as this is for a Phone app.

I am reluctant to post my code below, as it is not needed to ask the question but I will anyway. It's a bit messy. I hope to become more efficient in as3. I really need all the resourses not to be used up so I am hoping my script is not horrible in this area.

import flash.display.MovieClip;

var daCol:int; 
var daRow:int;
var daPosition:int;
var daPage:int;
var daWidth:int;
var daHeight:int;
var daTotalFlags:int;
var daTotalPages:int;


var daFlag:Array = [];
var daFlagText:Array = [];
var pages:Array = [];

// Movie Clips
var newBox:MovieClip = new MovieClip;


// Make new Movie clip to hold the flags
var flagHolder = new MovieClip();
flagHolder.name = "flagHolder";


for (var a:uint = 0; a < DirArray.length; a++)
{
    //trace(DirArray[a]);


    var Flag = new languageFlag();
    Flag.name = DirArray[a];
    daFlag.push(Flag);
    /// Important for adding flag to display
    //flagHolder.addChild(Flag);
    Flag.width = 160;
    //Flag.scaleX = 1.5;
    //Flag.scaleY = 1.5;
    daHeight = Flag.height;
    daWidth = Flag.width;

    daCol =  Math.floor(stage.stageWidth / Flag.width);
    daRow = Math.floor(stage.stageHeight / Flag.height)-1;



    trace("Flag Width: " + Flag.width);
    //Flag.x = Flag.width * a;
    theFlag = new Loader();
    theFlag.load(new URLRequest("Languages/"+DirArray[a]+"/Flag.png"));
    theFlag.name = DirArray[a];
    Flag.addChild(theFlag);
    //daFlagText = 
    Flag.btext.text = DirArray[a];
    theFlag.addEventListener(MouseEvent.CLICK, flagClick);


}

BG.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);

function fl_MouseClickHandler_2(event:MouseEvent):void
    {
    /// CALLS FUNCTIONS FOR FLAGS
    displayFlags();
    displayFlagButtons();   
    }



function displayFlags()
    {
    flagMoveIn();
    var theRow = 0;
    var C = 0;
    var goUp:int;
    var spaceHeight:int = 33;
    daHeight = daHeight + 20;


    /// DISPLAYS FLAG HOLDER
    addChild(flagHolder);

    /// Sets position of Flags in pages to start at zero
    daPosition = 33;

    daTotalFlags = daRow * daCol;
    daTotalPages =  daFlag.length/daTotalFlags;
    trace("DA TOTAL PAGES: " + daTotalPages);
    //////////////////////
    /////////////////////
    for(var p = 0; daTotalPages >= p; p++)
    {

        var screenPage:MovieClip = new MovieClip();
        pages.push(screenPage)

            for (var F:uint = 0; F <= daRow-1; F++)
                {


                    if(daFlag[F])
                    {

                        for(C = theRow; C < theRow+daCol; C++)
                        {




                            if(daFlag[goUp])
                            {

                            screenPage.addChild(daFlag[goUp]);
                            daFlag[goUp].y = daHeight * F;
                            daFlag[goUp].x = daFlag[goUp].width * C;

                            }
                            goUp++;

                        }

                    }

                }
    daPosition = daPosition + stage.stageWidth; 
    //--- End of work For Loop
    flagHolder.addChild(screenPage);
    } // End of Pages loop


    spaceFlags();
    }// End of Display Flags



/// This function orders the pages correctly and spaces them right 
/// and adds buttons to the pages at bottom
function spaceFlags(){


    for( var i = 0; pages.length > i; i++)
    {
    pages[i].scaleX = .9;
    pages[i].scaleY = .9;

    }


    newBox.addChild(pages[0]);

    for( var a = 0; pages.length > a; a++)
    {
    pages[a].x = stage.stageWidth/2 - newBox.width/2 + stage.stageWidth * a;

    }


    flagHolder.addChild(newBox);
}


    var flagButton:MovieClip;
    var flagArray:Array = new Array;
    var flagDisplayButtons:MovieClip = new MovieClip();
    function displayFlagButtons()
{




    for( var i = 0; pages.length > i; i++)
    {
        var flagButton:MovieClip = new roundButton();
        flagButton.x = (flagButton.width + 10) * i;
        flagButton.y = stage.stageHeight - flagButton.height;
        addChild(flagButton);
        flagButton.alpha = .3;

        flagDisplayButtons.addChild(flagButton);
        flagArray.push(flagDisplayButtons.getChildAt(i) as roundButton);
    }
    addChild(flagDisplayButtons);

    // Works
    flagDisplayButtons.x = stage.stageWidth/2 - flagDisplayButtons.width/2;




}

Solution

  • while ( movieclip.numChildren > 0 ) {
        if ( movieclip.getChildAt( 0 ) is Loader ) {
            // if you get an error for null objects here, add in a similar conditional for "is Bitmap"
            ( ( movieclip.getChildAt( 0 ) as Loader ).content as Bitmap ).bitmapData.dispose();
        }
        movieclip.removeChildAt( 0 );
    }
    

    That loop will go through and remove each child of your MovieClip (obviously change the name to match your code) and run dispose() on any Loader. BitmapData.dispose() will allow garbage collection to collect the data the next time it runs (there is no way to force this and seeing that change is difficult because of the way systems handle memory).

    You can obviously modify the loop as needed, but that should give you the general gist of what you need to do to unload each Loader from memory.