AS3 How to remove previous loaders

妖精的绣舞 提交于 2019-12-11 07:47:38

问题


In Flash CS4, I'm creating a photo gallery. My goal is to load different thumbnails from a number of images. I have managed that when one clicks an image, a number of thumbnails are being displayed, but when one clicks another image, the new thumbnails are placed on top of the old ones. Can someone help me on how to get rid of the old thumbnails?

Here is the code:

for (var i:int = 0; i < thumbnails.length(); i++) {
  imgLoader.unload();
  imgLoader = new Loader();
  imgLoader.load(new URLRequest(thumbnails[i]));
  imgLoader.name= i;
  imgLoader.x =  95 * columns;
  imgLoader.y = 80 * rows;
  imgLoader.alpha = 0;
  details.gallery.addChild(imgLoader);

  if (columns+1< 5) {
    columns++;
  } else {
    columns = 0;
    rows++;
  }
}

回答1:


This is where an Array is your friend. You could do this without an array by merely using a while loop to remove every last child from the sprite or movieclip that you added the thumbs to. The reason we use arrays is so that we can reuse the thumbs, instead of reloading them we merely remove them from the display list. You push a reference to each object into an array for each thumb as you add it to the display list. Each thumbContainer node in the XML gets its own array which get added to the main array. The main array holds references to thumbnail arrays. Thumbnail arrays hold references to loaded thumbnails so that they can be added and removed from the display list. If you plan to never use the thumbs after they have been seen once you may set it's reference equal to null, otherwise merely remove it from the display list; There is no reason to load it many times. When you are ready to add the new thumbs you must clear out previous thumbs. The easiest way to do this is with a while loop.

//Assuming the thumbs were loaded into container
while(container.numChildren > 0)
{
    //Remove the first child until there are none.
    container.removeChildAt(0);
}

//The XML / 2 Containers / thumbContainer[0] and thumbContainer[1]

<?xml version="1.0" encoding="utf-8"?>
<xml>
    <thumbContainer>
        <thumb path="path/to/file" />
        <thumb path="path/to/file" />
        <thumb path="path/to/file" />
    </thumbContainer>
    <thumbContainer>
        <thumb path="path/to/file" />
        <thumb path="path/to/file" />
        <thumb path="path/to/file" />
    </thumbContainer>
</xml>

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;

    public class DocumentClass extends Sprite
    {
        private var _container:Sprite;
        private var _mainArray:Array;
        private var _xml:XML;
        private var _urlLoader:URLLoader;
        private var _urlRequest:URLRequest;

        public function DocumentClass():void
        {
            if(stage) _init();
            else addEventListener(Event.ADD_TO_STAGE, _init, false, 0 , true);
        }
        private function _init(e:Event = null):void
        {
            //Will contain arrays for each thumbContainer in the XML.
            _mainArray = [];

            _urlRequest = new URLRequest('path/to/xml');
            _urlLoader = new URLLoader();
            _urlLoader.addEventListener(Event.COMPLETE, _onXMLComplete, false, 0, true);      
        }
        private function _onXMLComplete(e:Event):void
        {
            _xml = new XML(e.target.data);

            _loadThumbs(0);
        }
        private function _loadThumbs(pIndex:int):void
        {
            _clearThumbs();

            //Find out how many sets of thumbs there and add to _mainArray
            for(var i:int = 0; i < _xml.thumbContainer.length(); i++)
            {
                var tempArray:Array = new Array();

                for(var j:int = 0; j < _xml.thumbContainer[i].thumb.length; j++)
                {
                    tempArray[i].push(_xml.thumbContainer[i].thumb[j].@path);
                }
                _mainArray.push(tempArray);
            }


            //Here is where we add the new content to container, or you can call a function to do it.
        }
        private function _clearThumbs():void
        {
            while(container.numChildren > 0)
            {
                //Remove the first child until there are none.
                container.removeChildAt(0);
            }
        }
    }
}

Again, it is good practice to hold a reference to something that is reusable and to simply remove it from the display list instead of setting to null and prepping for garbage collection only to be loaded again later. I already have written more than I intended and wasn't able to slap in all the code I wanted. It is important to setup the code that makes sure it only loads a particular set of thumbs once; That is the whole idea. As for removing them, it's as simple as the while loop I showed you, you just need to know the name of the DisplayObjectContainer that parents them.




回答2:


You are adding new thumbnails to your gallery object via the addChild() methods, so you should call a method that removes all thumbnails from the gallery before adding new ones.




回答3:


I believe it is removeChild() you should use.

I had some very weird problems with this not long ago, causing all sorts of crazy hit collisions and what not. My solution were to call removeChild() on the object I wanted to remove, and then I set it to "null". Prior to this I had a check if the object were null (can't removeChild something that's null).

Can't guarantee this is how you "should" do it, as I'm pretty newbie to the Actionscript scene myself. Hope it solves your problem.




回答4:


You can remove them by passing the object into details.gallery.removeChild(object) or by index using details.gallery.removeChildAt(index). If you use removeChild(), be sure to check for a null or it will throw an error.

Make sure to check out the flash help files, they're by far the best resource you can use.




回答5:


This will remove all children within a movieclip called gallery:

while( gallery.numChildren > 0) { gallery.removeChildAt(0); };


来源:https://stackoverflow.com/questions/596660/as3-how-to-remove-previous-loaders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!