AS3如何删除以前的装载程序

时间:2009-02-27 20:55:30

标签: actionscript-3 actionscript flash-cs4

在Flash CS4中,我正在创建一个照片库。我的目标是从许多图像中加载不同的缩略图。我已经设法在单击图像时显示了许多缩略图,但是当单击另一个图像时,新缩略图将放在旧图像的顶部。有人可以帮助我摆脱旧的缩略图吗?

以下是代码:

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++;
  }
}

5 个答案:

答案 0 :(得分:2)

这是Array是你的朋友。您可以在没有数组的情况下执行此操作,只需使用while循环从您添加了拇指的精灵或动画片段中删除每个最后一个孩子。我们使用数组的原因是我们可以重复使用拇指,而不是重新加载它们,我们只是将它们从显示列表中删除。将每个对象的引用推送到每个对象的数组中,同时将其添加到显示列表中。 XML中的每个thumbContainer节点都有自己的数组,该数组将被添加到主数组中。主数组包含对缩略图数组的引用。缩略图数组包含对已加载缩略图的引用,以便可以在显示列表中添加和删除它们。如果您计划在看到它们之后永远不会使用它们,您可以将它的引用设置为null,否则只需将其从显示列表中删除;没有理由多次加载它。当您准备添加新拇指时,您必须清除以前的拇指。最简单的方法是使用while循环。

//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);
            }
        }
    }
}

同样,最好保留对可重用的东西的引用,并简单地将其从显示列表中删除,而不是设置为null并准备垃圾收集,以便稍后再次加载。我已经写了比我想要的更多,并且无法打入我想要的所有代码。设置代码非常重要,以确保它只加载一组特定的拇指;这就是整个想法。至于删除它们,它就像我向你展示的while循环一样简单,你只需要知道父对象的DisplayObjectContainer的名称。

答案 1 :(得分:0)

您正在通过addChild()方法向图库对象添加新的缩略图,因此您应该调用一个方法,在添加新缩略图之前从库中删除所有缩略图。

答案 2 :(得分:0)

我相信你应该使用removeChild()。

不久前我遇到了一些非常奇怪的问题,引发了各种各样的疯狂撞击事件。我的解决方案是在我想删除的对象上调用removeChild(),然后将其设置为“null”。在此之前,我检查了对象是否为null(无法删除孩子的null)。

无法保证这是你应该“做”的方式,因为我自己是Actionscript场景的新手。希望它能解决你的问题。

答案 3 :(得分:0)

您可以通过将对象传递给details.gallery.removeChild(object)或使用details.gallery.removeChildAt(index)索引来删除它们。如果你使用removeChild(),一定要检查null或者它会抛出错误。

请务必查看Flash帮助文件,它们是您可以使用的最佳资源。

答案 4 :(得分:0)

这将删除名为gallery的动画片段中的所有子项:

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