这就是数组是你的朋友的地方。您可以在没有数组的情况下执行此操作,只需使用一个 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 的名称。