将XML结果组织为容器中的单元格(AS3)

时间:2010-04-19 11:56:46

标签: xml flash actionscript-3

我在解决如何组织容器内单元格中的XML数据时遇到了一些问题。我确信这应该是AS3中的一个基本内容,但是我的脑袋已经油炸了......任何人都可以帮忙吗?

基本上是一个数组,如果被送到callThumbs(),它会遍历它并将这些条目与预加载的XML _my_images进行比较。如果找到匹配,则会将其发送到processXML,其中会加载所有相关信息并加载.jpg缩略图。然后将所有这些都馈送到createCell,这会创建一个特定的单元格,其位置值取决于x_countery_counter值(一行中有4个单元格),并将单元格添加到容器{{1 }}

问题:一切正常并且看起来很好,问题是容器内的单元格不按降序显示。它们是随机顺序的,可能是因为某些.jpg的加载时间较长等。如何通过XML .id值以降序轻松组织容器中的单元格?或者我如何告诉Flash等到加载缩略图和数据并创建和添加单元格?

谢谢大家,非常感谢所有的帮助!

PJ

_container_mc

1 个答案:

答案 0 :(得分:1)

为了让flash等待每个缩略图,您必须等待每个缩略图加载才能加载下一个缩略图。您应该能够轻松修改代码来处理这个问题。下面的代码可能需要一些更改,我只是想说明我的观点。

(AS3)
   // create these outside of the functions so they can be updated/used by both
   var i:Number = 0;
   var totalResults:Number = _my_results.length;

   // start loading the first thumbnail
   callThumbs(0);

   function callThumbs(resIndex:Number):void { 

        var _thumb_url:XML;
        var result = _my_results[resIndex];

        for (var n:Number = 0; n < _my_images.length(); i++) {

            if (_my_images[n].@id.toXMLString() == result) {

                _thumb_url = _my_images[n];

                processXML(_thumb_url, n);

                /**
                 * break here because we don't want to do another iteration
                 * the complete handler for the thumbnail loader will determine if this function
                 * is to be called again.
                 */
                break;
            }

        }

    } // End callThumbs


    function processXML(imageXML:XML, num:Number) {

        var _thumb_loader = new Loader();

        _thumb_loader.load(new URLRequest("thumbs/thumb_sm/" + imageXML.@id + "_st.jpg"));
        _thumb_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,thumbLoaded);
        _thumb_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, urlNotFound);

        var id:XMLList = new XMLList;
        id = imageXML.@id;
        var description:XMLList = new XMLList;
        description = imageXML.@description;

        function urlNotFound(event:IOErrorEvent):void {
            trace("The image URL '" + String(imageXML.@id) + "' was not found.");
        }

        function thumbLoaded(e:Event):void {
                var imageLoader:Loader = Loader(e.target.loader);
                var bm:Bitmap = Bitmap(imageLoader.content);

                createCell(bm, id, description, num);
                adjustFooterBar(); // Adjust bottom footer

            // determine if there is another thumbnail to be loaded
            if (i < totalResults) {
                i++;
                callThumbs(i);
            }

        }


    } // End processXML



    private function createCell(_image:Bitmap, _id, _description:String, _position):void { // Creates a cell with data, add to container

        var _cell_mc = new CellTitle();         

        _cell_mc.initCell(_image, _id, _description, _position, x_counter, y_counter);

        if (x_counter+1 < 4) {
            x_counter++;
        } else {
            x_counter = 0;
            y_counter++;
        }

        _container_mc.addChild(_cell_mc); // movieclip container

    } // End createCell

另外,出于好奇,这是在时间轴上完成的吗?