as3 removeChild和Loaders in?

时间:2013-05-03 16:51:41

标签: actionscript-3 classloader removechild

我正在创建多个加载器以使用for循环加载图像。然后我将这些图像放在一个新的MoveClip中。

需要时我要删除孩子(flagHolder);其中包含加载器中加载的标志的所有图像。 我想知道这会删除加载到我要移除的父影片剪辑中的所有myLoaders吗?

我希望这可能不会占用资源,因为这是一个电话应用程序。

我不愿意在下面发布我的代码,因为不需要问这个问题,但无论如何我都会。这有点乱。我希望在as3中变得更有效率。我真的需要所有的资源不要用完,所以我希望我的剧本在这个领域并不可怕。

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;




}

1 个答案:

答案 0 :(得分:1)

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 );
}

该循环将遍历并删除MovieClip的每个子项(显然更改名称以匹配您的代码)并在任何dispose()上运行LoaderBitmapData.dispose()将允许垃圾收集在下次运行时收集数据(由于系统处理内存的方式,无法强制执行此操作并且看到更改很困难。)

显然你可以根据需要修改循环,但这应该为你提供了从内存中卸载每个Loader所需要做的一般要点。