A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Removing clips using array

  1. #1
    Junior Member
    Join Date
    Jan 2008
    Posts
    27

    Removing clips using array

    If I use array to reference clips, what's the best way to remove the clips using that array?

    Here's the basic example code with a way that seems to work. I want to know the best/proper way to remove from memory. Thanks!

    Code:
    var arrayListItems:Array = new Array();
    
    function loadList(){
    	for(var i:Number=0;i < totalListItems; i++) {
    		var itemHolder:ListItem = new ListItem;
    		holderList.addChild(itemHolder);
    	}
    	arrayListItems.push(itemHolder);	
    	itemHolder = null;
    }
    
    function unloadList():void {
    	for(var i:Number=0;i<arrayLoadersPics.length;i++){
    		holderList.removeChild(arrayListItems[i]);
    	}
    	arrayListItems = [];
    }

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    That's not altogether horrible, but it does have a fairly serious bug. You need to push the item into the array inside the loop. Otherwise, you'll only get a single item in the array when you call loadList, even though all of them are added as children to holderList.

    Other minor points:
    • itemHolder is not a holder. It's an item.
    • holderList is not a list, it's some sort of DisplayObjectContainer.
    • there's no need to set itemHolder to null at the end of loadList, since that variable is local to the function. The item it refers to is also not eligible for garbage collection yet since it is referred to both in the array and the display list.
    • When creating a new array, it's slightly more efficient to use the empty array literal [] than new Array().
    • when calling a constructor, use the (), even if it takes no argument. This is stylistic only.
    • give types to all variables and return types to all functions
    • for loop index variables should be int rather than Number
    • iterate over the array when removing things from the array, why did you use arrayLoaderPics.length?


    Code:
    var items:Array = [];
    
    function loadList():void{
    	var item:ListItem;
    	for(var i:int=0;i < totalListItems; i++) {
    		item = new ListItem();
    		holder.addChild(item);
    		items.push(item);	
    	}
    }
    
    function unloadList():void {
    	for(var i:int=0;i<items.length;i++){
    		holder.removeChild(items[i]);
    	}
    	items = [];
    }
    Instead of setting items to a new empty array at the end of unloadList, you may want to pop items off the array and remove them like that.
    Code:
    function unloadList():void {
    	while(items.length > 0){
    		holder.removeChild(items.pop());
    	}
    }

  3. #3
    Junior Member
    Join Date
    Jan 2008
    Posts
    27
    Thanks for the info!

    The push statement outside of the loop was a typo. This code is part of larger code.

    holderList and ListItem are both custom clips. Should I avoid using "list" and "item" in names?

    I used arrayLoadersPics.length because in the original code I'm storing the image loaders in an array and removing them in the same loop. ListItem is a clip with thumbnail and text. Is that what I should be doing with loaders? Store the loaders in an array and unload them when I remove the thumbnails?

    Does using pop() have an advantage in memory or collection over resetting items to empty array?

    Thanks!
    Last edited by schlag; 12-05-2011 at 09:12 PM.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I think Item is fine. I would avoid List for things that are not collections. But that's just a matter of naming. You could call it babysmasher and it would function just the same.

    Using arrayLoadersPics.length as your iteration limit, then having your loop actually manipulate a different array seems wrong. It's likely to go wrong if those arrays have different lengths.

    The difference between using pop and resetting to an empty array is that using pop will preserve the same array. If you have any other references to it, they will remain valid. Throwing that array away and creating a new one will mean that any other references to the old array will not be affected by whatever you do with the new array.
    If you have no other references, then it's a toss up. I don't know which way is more efficient in practice. I suspect creating a new array would be, since that's a fairly primitive operation in flash, and it would avoid any array bookkeeping necessary with using pop.

  5. #5
    Junior Member
    Join Date
    Jan 2008
    Posts
    27
    Makes sense. Thanks!

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