dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Need help with removing attached movieclips

  1. #1
    Member
    Join Date
    May 2011
    Posts
    55

    Need help with removing attached movieclips

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

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



    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 //):

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



    ... 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.

  2. #2
    Member
    Join Date
    May 2011
    Posts
    55
    Never mind, problem solved, I just rewrote everything and did it in an entirely different way so I then could more easily manage things

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I hope you realized that you didn't need instance names at all. All you need is references to the objects.

    I wish adobe had made the name property read-only. If only the flash IDE could set names, then so much ugly ugly ugly code would never have come about.

  4. #4
    Senior Member guardiankitty's Avatar
    Join Date
    Dec 2006
    Location
    Here
    Posts
    215
    Quote Originally Posted by 5TonsOfFlax View Post
    I hope you realized that you didn't need instance names at all. All you need is references to the objects.

    I wish adobe had made the name property read-only. If only the flash IDE could set names, then so much ugly ugly ugly code would never have come about.
    Why do you not like instance names (in the flash IDE I presume)? On a side note I literally have never gotten a child by its .name property- never really had a good reason to.

    ps. I am just curious if I have been doing stuff poorly, maybe you have a better idea on how to go about doing stuff.
    Last edited by guardiankitty; 01-06-2012 at 08:44 PM.

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I think you misunderstand. In the IDE, when graphically placing instances on the stage, is the only time instance names should be allowed to be set (and then only because that's how you get a property automatically added). After that, the reason to disallow it is exactly what your experience says: there's no good reason to use the name property.
    If you're using it to get a reference with getChildByName, then instead use an actual reference such as a property value.
    If you're using naming schemes to mock a collection (box1, box2, box3...), then instead use an actual collection such as array.

    Basically, there's always a better way to do whatever you're doing that will actually tell you if you have a typo or other stupid error. And it'll be more efficient too.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center