0

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

需要时我将删除Child(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;




}
4

1 回答 1

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()在任何Loader. BitmapData.dispose()将允许垃圾收集在下次运行时收集数据(没有办法强制执行此操作,并且由于系统处理内存的方式,很难看到更改)。

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

于 2013-05-03T17:04:43.710 回答