如何一次加载xml项目as3

时间:2009-11-18 05:08:12

标签: xml flash actionscript-3

Flash AS3:

有没有人知道如何一次加载一个项目而不是在使用AS3的正常过程中加载循环?我有覆盖价值的问题。

我无法完成这项工作,它总是覆盖最后一个值。我试图使用字典,虽然它有助于解决我的问题......

当我点击它时,我希望能够使用EACH按钮显示我的标记的可见性。

private function handleMarkers(event:CustomEventCenter):void
    {
        // Get items info from custom dispatcher
        var nom     = event._name.nom;
        ...

        var tabMarkerID:Array   = new Array();

        // Defined positions for button
        var xPos = 20;
        var yPos = 0;

        // Store the right markerID into each index separated
        if (event._name.markerID == '01') {
            tabMarkerID[0] = event._name.markerID;
        } else if (event._name.markerID == '05') {
            tabMarkerID[1] = event._name.markerID;
        }

        // Create a dictionary  
        var dict:Dictionary = new Dictionary(); 

        // Create arrondissement objects for 14 available markers
        var arrond1:Object = new Object();
        var arrond2:Object = new Object();

        dict[arrond1] = tabMarkerID[0];
        dict[arrond2] = tabMarkerID[1];

        for (var i=0;i<2;i++) 
        {
            pointRepere = new PointRepere();
            for (var item:Object in dict)
            {
                if (dict[item] != undefined) {          
                    pointRepere.name = String(dict[item]); // here the issue
                    pointRepere.x = xPos;
                    pointRepere.y = yPos;
                    yPos += i*20 + 40;
                    pointRepere.buttonMode = true;

                    addChild(pointRepere);
                    createMarkers(dict[item], nom);
                    pointRepere.addEventListener(MouseEvent.CLICK, handlePointClicked);
                }
            }
        }

    }


    private function handlePointClicked(event:MouseEvent):void
    {
        trace("Name: "+event.target.name);

        displayMarkerType(event.target.name);
    }


    private function displayMarkerType(id:String):void
    {
        for (var i=0; i< _marker.tabArrondMarker[id].markers.length; i++)
        {
            var marker:Marker = _marker.tabArrondMarker[id].markers[i];
            (!marker.visible) ? marker.visible = true : marker.visible = false; 
        }
    }

2 个答案:

答案 0 :(得分:1)

您似乎想要延迟加载xml文件 - 您需要构建一个基于您要加载的项目数组运行的循环 - 以下是您可以执行此操作的示例:

private var Array:list;
private var int:count = 0;

private function init()void
{
     list = {"one.xml","two.xml","three.xml"};
     loadXML();
}

private function loadXML():void
{
     //create and init your loader
     var loader:URLLoader = new URLLoader(new URLRequest(list[count]));
     loader.addEventListener(Event.COMPLETE, completeHandler);
     loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
     loader.load();
}

private function completeHandler(e:Event):void
{
     //Logic here to handle the loaded xml
     continueLoad();
}

private function ioErrorHandler(e:IOErrorEvent):void
{
     //Logic here to handle the error
    continueLoad();
}

private function continueLoad():void
{
     if(count < list.length){
          count++;
          loadXML();
     }else{
          //Some logic here to do something else after all of your files have been loaded
     }
}

我只是把它放在这里,所以你可能需要修复一两件事,但你看到了要点 - 这是一个循环,只有在每个单独的文件加载后执行,而不是只是在for循环中抛出它并让它执行并随后研磨然后崩溃。

希望这有帮助!

答案 1 :(得分:0)

您可以加载所有内容,然后按索引引用它们。像

xmlData.YourChildNodeName[0] 

其中0是索引号。这只会以该名称拉回第一个子节点。