Skip to main content
Inspiring
January 6, 2012
Question

Need help with removing attached movieclips

  • January 6, 2012
  • 2 replies
  • 818 views

Hi, I'm creating a highscore list like this:

[AS]function loaderCompleteHandler(e:Event):void {

    var scoreXML:XML=new XML(e.target.data);

    var playerResult:MovieClip;

    var pX:Number=86;

    var pY:Number=116;

        var order:Number=0;

    for each (var player:Object in scoreXML.player) {

        order+=1;

        playerResult = new PlayerResult();

        playerResult.x=pX;

        playerResult.y=pY;

        playerResult.order_t.text=" "+order;

        playerResult.name_t.text=player.@username;

        playerResult.score_t.text=player.score.@score;

        addChild(playerResult);

        pY+=playerResult.height-1;

        trace(playerResult);

    }

}[/AS]

I have a MovieClip with the linkage name of "playerResult" in the library. For every loop it takes the scores+names from the xml and displays them by creating a new instance of "playerResult" and attaches them one by one under each other.

I need to be able to remove this highscore when it's not longer needed, thus (the most important part) freeing up memory. So I can then use the same code to show the scores again without doubling the memory it uses every time.

I just can't find a good way to remove all the MCs as they all get the same instance name for every new mc attached. So I added a line (marked with //):

[AS]function loaderCompleteHandler(e:Event):void {

    var scoreXML:XML=new XML(e.target.data);

    var playerResult:MovieClip;

    var pX:Number=86;

    var pY:Number=116;

    var order:Number=0;

    for each (var player:Object in scoreXML.player) {

        order+=1;

        playerResult = new PlayerResult();

        playerResult.name="playerResult"+[order]; // THIS ONE

        playerResult.x=pX;

        playerResult.y=pY;

        playerResult.order_t.text=" "+order;

        playerResult.name_t.text=player.@username;

        playerResult.score_t.text=player.score.@score;

        addChild(playerResult);

        pY+=playerResult.height-1;

        trace(playerResult);

    }

}[/AS]

... so I now have a name for each instance (playerResult0, playerResult1...) but still can't come up with a way to remove them.

The scores are displayed like this:

1 name       score

2 name       score

etc...

Any suggestions?

thanks.

This topic has been closed for replies.

2 replies

January 6, 2012

if(this.getChildByName("instancname") != null)

{

this.removeChild(this.getChildByName("inastance name"))

}

TenchyMyoAuthor
Inspiring
January 6, 2012

Thanks for the help, it now works as I want. This is what I did:

function loaderCompleteHandler(e:Event):void {

     var playerResultArray = new Array();

     var scoreXML:XML=new XML(e.target.data);

     var playerResult:MovieClip;

     var pX:Number=86;

     var pY:Number=116;

     var order:Number=0;

     var startNumber:Number=0;

     var clearArray:Boolean=false;

     for each (var player:Object in scoreXML.player) {

         //trace(player.@username);

         order+=1;

         playerResult = new PlayerResult();

         playerResult.x=pX;

         playerResult.y=pY;

         playerResult.order_t.text=" "+order;

         playerResult.name_t.text=player.@username;

         playerResult.score_t.text=player.score.@score;

         addChild(playerResult);

         pY+=playerResult.height-1;

         playerResultArray.push(playerResult);

     }

     b.addEventListener(MouseEvent.CLICK, bHandler);

     function bHandler(e:MouseEvent) {

         for (var i:int = 1; i <= order; i++) {

             removeChild(playerResultArray[startNumber]);

             startNumber+=1;

             if (startNumber==order) {

                 b.removeEventListener(MouseEvent.CLICK, bHandler);

                 clearArray=true;

             }

         }

         if (clearArray==true) {

             for (var ii:int = 1; ii <= order; ii++) {

                 playerResultArray.pop();

             }

         }

     }

}

Adding the instances to an array. When I feel like removing the instances + clear the array, I push the button "b" (this code was tricky for me to come up with, but it worked after many attempts). This code (like I have to explain :P) checks how many times it needs to run the for loops. The "i" for loop removes the instances and then toggles clearArray to true to execute the next for loop "ii" to remove the stuff from the array. I first did it so it would remove the stuff from the array one by one per each for loop "i" but got errors so realized that everytime it popped an item, the order of things in the array changed, so I came up with making two for loops so when one was done, it could start on the next.

It all works the way I want now, but if you have any suggestions, maybe optimizations, please do tell

Thanks again.

TenchyMyoAuthor
Inspiring
January 6, 2012

Hmm, I don't get restored memory.

Scenario:

I start the app, click to display the stuff, 30kb of memory is used. I click on button to remove the stuff, no memory gets freed. I click on the button to display the stuff again, another 30kb of memory is used, I click on remove the stuff and no memory is freed.

I start the app, click to display the stuff, 30kb of memory is used. Now if I click on the button to display the score again (while it's already displaying), no memory is being used.

Weird. Why don't I get some memory freed up when clicking "b" button to remove the score stuff?

Here is the full code, if it helps:

function getScores():void {

    var url:String="https://www.scoreoid.com/api/getScores";

    var request:URLRequest=new URLRequest(url);

    var requestVars:URLVariables=new URLVariables();

    request.data=requestVars;

    requestVars.response="XML";

    requestVars.limit=4;

    requestVars.order_by="score";

    requestVars.order="dsc";

    request.method=URLRequestMethod.POST;

    var urlLoader:URLLoader = new URLLoader();

    urlLoader = new URLLoader();

    urlLoader.dataFormat=URLLoaderDataFormat.TEXT;

    urlLoader.addEventListener(Event.COMPLETE,loaderCompleteHandler);

    urlLoader.load(request);

}

function loaderCompleteHandler(e:Event):void {

    var playerResultArray = new Array();

    var scoreXML:XML=new XML(e.target.data);

    var playerResult:MovieClip;

    var pX:Number=86;

    var pY:Number=116;

    var order:Number=0;

    var startNumber:Number=0;

    var clearArray:Boolean=false;

    for each (var player:Object in scoreXML.player) {

        //trace(player.@username);

        order+=1;

        playerResult = new PlayerResult();

        playerResult.x=pX;

        playerResult.y=pY;

        playerResult.order_t.text=" "+order;

        playerResult.name_t.text=player.@username;

        playerResult.score_t.text=player.score.@score;

        addChild(playerResult);

        pY+=playerResult.height-1;

        playerResultArray.push(playerResult);

    }

    b.addEventListener(MouseEvent.CLICK, bHandler);

    function bHandler(e:MouseEvent) {

        for (var i:int = 1; i <= order; i++) {

            removeChild(playerResultArray[startNumber]);

            startNumber+=1;

            if (startNumber==order) {

                b.removeEventListener(MouseEvent.CLICK, bHandler);

                clearArray=true;

            }

        }

        if (clearArray==true) {

            for (var ii:int = 1; ii <= order; ii++) {

                playerResultArray.pop();

            }

        }

    }

}

a.addEventListener(MouseEvent.CLICK, aHandler);

function aHandler(e:MouseEvent) {

    getScores();

}

Ned Murphy
Legend
January 6, 2012

To target an object using its name property you need to use the getChildByName() method.  Another way to approach this is to add all the playerResult instances to a movieclip.  You can then just use a loop to remove all the children from that movieclip using getChildAt(0) until there are no children left--no need for a name.  Another way to do this is to assign each instance to an array and loop thru the array... in that case it is usually best to remove them from the far end rather than starting at 0.