从xml读取数据 - 只读最后一个值

时间:2013-02-12 17:17:14

标签: xml actionscript-3 flash actionscript flash-cs5

我有xml文件,其中包含名称,描述和Xcor,Ycor等数据。 我在这个地方的舞台上做了每个对象显示什么写在xml X和Ycor 我还为每个不同的MC制作了其他MC - 它运作良好 但是当我尝试使用选项跟踪在rollOver之后显示MC的名称时 - 它总是显示名称“Karkow” - 但是错误因为每个MC在xml文件中都有不同的名称

你知道为什么克拉科夫总是反对其他名字吗?

这是我的xml代码:

  <?xml version="1.0" encoding="utf-8" ?> 
- <XML>
- <myXMLList>
- <object>
  <name>Warszawa</name> 
  <opis>jakis opis</opis> 
  <Xcor>100</Xcor> 
  <Ycor>50</Ycor> 
  </object>
- <object>
  <name>Wroclaw</name> 
  <opis>jakis opis2</opis> 
  <Xcor>20</Xcor> 
  <Ycor>200</Ycor> 
  </object>
- <object>
  <name>Krakow</name> 
  <opis>jakis opis3</opis> 
  <Xcor>50</Xcor> 
  <Ycor>250</Ycor> 
  </object>
  </myXMLList>
  </XML>

这是我的as3代码:

import flash.display.MovieClip;
import flash.display.Shape;
import flash.accessibility.Accessibility;
import flash.display.DisplayObject;
import flash.geom.Point;
import flash.events.Event;
import flash.events.MouseEvent;




// Initialize the XML, place the xml file name, initialize the URLRequest
// put URLRequest into a new URLLoader, and add event listener on 
// myLoader listening for when the XML loading is complete
var myXML:XML = new XML();
var XML_URL:String = "miasta.xml";
var myXMLURL:URLRequest = new URLRequest(XML_URL);
var myLoader:URLLoader = new URLLoader(myXMLURL);
myLoader.addEventListener("complete", xmlLoaded);

// Create the xmlLoaded function
function xmlLoaded(event:Event):void {

    // Place the xml data into the myXML object
    myXML = XML(myLoader.data);
    // Initialize and give var name to the new external XMLDocument
    var xmlDoc:XMLDocument = new XMLDocument();
    // Ignore spacing around nodes
    xmlDoc.ignoreWhite = true;
    // Define a new name for the loaded XML that is the data in myLoader
    var menuXML:XML = XML(myLoader.data);
    // Parse the XML data into a readable format
    xmlDoc.parseXML(menuXML.toXMLString());

         // Run the "for each" loop to iterate through all of the menu items listed in the external XML file
         for each (var object:XML in myXML..object) {

                 // Access the value of the "name" node in our external XML file
                 var name:String = object.name.toString();
                 // Access the value of the "opis" node in our external XML file
                 var opis:String = object.opis.toString();
                 // zmiana koordynat na string
                 var Xcor:String = object.Xcor.toString();
                 var Ycor:String = object.Ycor.toString();
                 //change coordinate to number
                 var XcorNum = Number(Xcor);
                 var YcorNum = Number(Ycor);



                 if (name == "Warszawa") {
                     var myMovieClip:MovieClip = new Policja();


                 }

                 else if (name == "Wroclaw") {

                 var myMovieClip:MovieClip = new owal();

                 }

                  else if (name == "Krakow") {

                 var myMovieClip:MovieClip = new city();

                 }
addChild(myMovieClip);
// Set the location of the new MovieClip
myMovieClip.x = XcorNum;
myMovieClip.y = YcorNum;

myMovieClip.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler);



myMovieClip.buttonMode = true;
myMovieClip.useHandCursor = true;




function onRollOverHandler(myEvent:MouseEvent){
    trace (name);
}

2 个答案:

答案 0 :(得分:0)

因为很可能你的MouseEvent-handler被定义为一个闭包。现在,对name的每次访问都是指定名称的最后一个值。

我建议将名称指定为MovieClip的属性,并通过此方式使用RollOverHandler访问它

...
for each (var object:XML in myXML..object) {
    var name:String = object.name.toString();

    if (name == "Warszawa") {
        var myMovieClip:MovieClip = new Policja();
    }
    else if (name == "Wroclaw") {
        var myMovieClip:MovieClip = new owal();
    }
    else if (name == "Krakow") {
        var myMovieClip:MovieClip = new city();
    }

    addChild(myMovieClip);
    myMovieClip.cityName = name;
    myMovieClip.addEventListener(MouseEvent.ROLL_OVER, 
        function (myEvent:MouseEvent){
            trace (this.name);
        });
}

ps:我认为你的例子至少缺少一个结束“}”

答案 1 :(得分:0)

好的,现在正在工作 我只是添加

myMovieClip.cityName = name;

为每个myMovieClip, 并在最后

function onRollOverHandler(e:MouseEvent){
    trace (MovieClip(e.currentTarget),name);
}

感谢您的帮助