简体   繁体   English

AS3-无序XML IMG加载

[英]AS3 - Unordered XML IMG loading

I have this code that loads thumbs and full images in my project: 我有这段代码可以在我的项目中加载缩略图和完整图像:

private function processXMLHandle(e:Event):void
    {
        var xml:XML = new XML(e.target.data);

        for each (var line:XML in xml.IMAGE)
        {
            var file:String = line.@THUMB;              

            var loader:Loader = new Loader();
            loader.load(new URLRequest(file));

            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);

            var fileFull:String = line.@FULL;

            var loaderFull:Loader = new Loader();
            loaderFull.load(new URLRequest(fileFull));
            loaderFull.contentLoaderInfo.addEventListener (Event.COMPLETE, completeFullHandler);

        }

        myXMLLoader.removeEventListener(Event.COMPLETE, processXMLHandle);
    }

When I push the array I'v created for that, the images comes in an unsorted order. 当我推送为此创建的数组时,图像以未排序的顺序出现。

I friend of my told me that It's happening something like that: "the small files are comming first". 我的一个朋友告诉我,正在发生类似的事情:“小文件首先出现”。 He told me that the problem is with the loader, but he can't help-me. 他告诉我问题出在装载机,但他帮不了我。

Can anyone tell-me what's wrong? 谁能告诉我怎么了?

Thanks! 谢谢!

The best way to approach this is with a bit more structure to your code. 最好的方法是在代码中增加一些结构。

First, create a class (let's call it ImageLoader) responsible for loading both the thumb and the full image. 首先,创建一个类(负责将其称为ImageLoader)来加载缩略图和完整图像。 This would be a class with 2 Loader instances. 这将是一个具有2个Loader实例的类。 Create an instance of this class for each item, and push them into an array before you start. 为每个项目创建此类的实例,然后在开始之前将它们推入数组。

Once the array is full, iterate the array instructing the ImageLoader instances to start. 阵列装满后,请对阵列进行迭代,以指示ImageLoader实例启动。 This method would in turn call the load method of each one of its Loader instances. 该方法将依次调用其每个Loader实例的load方法。

Once both the Loaders have completed, emit a custom ImageLoaderComplete event indicating that it is complete. 两个加载器都完成后, 发出一个自定义ImageLoaderComplete事件,指示该事件已完成。

At a higher level, in the same class as contains the array of ImageLoader instances, listen for the custom event emited from each of the ImageLoader instances. 在更高级别上,在与包含ImageLoader实例数组的类相同的类中,侦听从每个ImageLoader实例发出的自定义事件。 Count them in. When you've counted as many as there are items in the array, loading is complete, and your array remains in the same sequence as when you started, with the thumb and full image conveniently grouped. 算上它们。计数完阵列中的所有项目后,加载完成,阵列保持与开始时相同的顺序,并方便地将拇指和完整图像分组。

It looks like you are looping through a given XML document that contains a list of images and loading the images asyncronously in a loop. 看起来您正在遍历包含图像列表的给定XML文档,并以循环方式异步加载图像。 In doing that, obviously the smaller images are going to finish first causing the Event.COMPLETE event to fire. 这样做显然会导致较小的图像首先完成,从而引发Event.COMPLETE事件。 This happens because you are asking flash to start loading all the images immediately and concurrently. 发生这种情况是因为您要求Flash立即开始并发加载所有图像。

What you may want to do is instead load up the images synchronously in a loop by first loading the first image...when that images Event.COMPLETE executes, continue to load the next image and so on until they are all done. 您可能想要做的是改为通过首先加载第一个图像来循环加载图像...在执行Event.COMPLETE图像时,继续加载下一个图像,依此类推,直到全部完成。 Doing this, will force your application to load the images in the order defined in the XML and one at a time sort of like a queue. 这样做将迫使您的应用程序按照XML中定义的顺序加载图像,并且每次都像队列一样加载图像。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM