0

我有 xml 文件,其中包含名称、描述和 Xcor、Ycor 等数据。我在这个地方的舞台上制作了每个对象显示在 xml X 和 Ycor 中我还为每个不同的名称制作了其他 MC - 它运行良好但是当我尝试选项跟踪以显示 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);
}
4

2 回答 2

0

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

myMovieClip.cityName = 名称;

对于每个 myMovieClip,最后

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

感谢帮助

于 2013-02-13T09:37:34.320 回答
0

因为很可能您的 MouseEvent 处理程序被定义为一个闭包。现在,对 name 的每次访问始终是 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:我认为您的示例至少缺少一个结束“}”

于 2013-02-12T19:46:30.483 回答