views:

213

answers:

2

Flash AS3:

Does anyone know how to load items one at a time instead of loading in loop at a normal process using AS3? I'm having problem with overwritten value.

I couldn't make that work, it's always overwritten the last value.I tried to use the dictionary and though it will help to resolve my problem...

I want to be able for EACH button when I clicked it displays the visibility of my markers.

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; 
  }
 }
A: 

You can load them all, and then reference them by their index. Like

xmlData.YourChildNodeName[0]

Where the 0 is the index number. This will just pull back the first child node by that name.

Joey Blake
Then can I load to next one?One item dynamicly at time I believe I need a counter
Qpixo
+1  A: 

It seems like you'd want to lazy load your xml files - you'd need to construct a loop that runs based on an array of the items you want to load - here is an example of how you could do that:

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

I just this threw this together here so you may need to fix a thing or two but you see the gist - it's a loop that executes only after each individual file has been loaded as opposed to just throwing it all in a for loop and having it execute and subsequently grind and then crash.

Hope this helps!

onekidney
Unlike your example which is really good tip. Mine is only loaded ONE XML file instead and I need to find a way not to overwritten any value loaded in xml attributes and name.Let's say xml file structure:<map> <point id="01"> <name>testing 1</name> ... </point> <point id="02"> <name>Test2</name> ... </point> ...</map>
Qpixo