0

我正在使用 Adob​​e AIR 3.3(适用于 iOS)在 Flash CS6 中工作,并且无法将 XML 字符串返回到 textField。

它正在跟踪正确的信息,我尝试了几种方法来返回跟踪,但似乎无法完全理解......这是我最近的尝试。有什么建议么?提前致谢。

    var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("http://www.someURL.php"));
//php file that echos xml   
myLoader.addEventListener(Event.COMPLETE, init);
     var fadedText:TextField;
     var touchList:TouchList;
     var textOutput:TextField;
     var animateLeft:Tween;
     var listArray:Array;
     var item:TouchListItemRenderer;
     var theXML:XML; 
     var days:Array = new Array("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday");
     var daySelected;
     var businessNameArray:Array = new Array();
     var businessLogoArray:Array = new Array();
     var businessAddress:Array = new Array();
     var distanceArrayDisplay:Array = new Array();
     var distanceArrayCount = 0;

     function init(e:Event = null):void
    {
        trace(myLoader.data);
        theXML = new XML(e.target.data);
        theXML.ignoreWhitespace = true;
        myLoader.close();


        // add our list and listener
        var itemSizeCalculator = stage.stageHeight / 6;
        touchList = new TouchList(stage.stageWidth, stage.stageHeight-itemSizeCalculator);
        touchList.addEventListener(ListItemEvent.ITEM_SELECTED, handlelistItemSelected);
        addChild(touchList);
        touchList.x = stage.stageWidth;


        // Fill our list with item rendreres that extend ITouchListRenderer. 
                for(var i:int = 0; i < theXML.food.length(); i++) {
                    if(theXML.food[i].monday != "un")
                    {                           
                item = new TouchListItemRenderer();
                item.index = i;
                item.data = theXML.food[i].business;
                item.name = theXML.food[i].business;
                item.addEventListener(MouseEvent.CLICK, itemWasClicked);
                item.itemHeight = itemSizeCalculator;

     businessNameArray[i]= theXML.food[i].business;
     businessLogoArray[i]=("http://www.logosURL.com/"+theXML.food[i].logo);
     businessAddress[i]= theXML.food[i].address;


            var fadedTextFormat:TextFormat = new TextFormat();
            fadedTextFormat.bold = true;
            fadedTextFormat.color = 0x999999;
            fadedTextFormat.size = 14;
            fadedTextFormat.font = "Helvetica"; 

            fadedText = new TextField();
            fadedText.height = 30;
            fadedText.mouseEnabled = false;
            fadedText.defaultTextFormat = fadedTextFormat;

            item.addChild(fadedText);
            fadedText.x = 75;
            fadedText.y = 10;
            distanceArrayDisplay.push(fadedText);

                var distanceLoader:URLLoader = new URLLoader();
                distanceLoader.load(new URLRequest("http://maps.googleapis.com&origins=someAddress&destinations="+businessAddress[i]+"&mode=walking&language=en-en&sensor=false"));
                distanceLoader.addEventListener(Event.COMPLETE, distanceCalculated);


                var logoLoader:Loader = new Loader();
                item.addChild(logoLoader);
                var logoURL:URLRequest = new URLRequest("http://www.myLogos.com/"+theXML.food[i].logo);
                logoLoader.load(logoURL);
                logoLoader.scaleX = .4;
                logoLoader.scaleY = .4;
                logoLoader.y = logoLoader.y + 5;
                logoLoader.mouseEnabled = false;
                var arrowGraphic:rightArrow = new rightArrow();
                item.addChild(arrowGraphic);
                arrowGraphic.x = stage.stageWidth - 5;
                arrowGraphic.y = item.height/2;

            touchList.addListItem(item);
                    }
        }
}


function distanceCalculated(e:Event)
{
//  trace(e.currentTarget.data);
var distanceXML:XML = new XML(e.target.data);
distanceXML.ignoreWhitespace = true;
var returnVar:String = (distanceXML.row.element.distance.text);
distanceArrayDisplay[distanceArrayCount].text = returnVar;
trace(returnVar);

distanceArrayCount++;
}
4

1 回答 1

0

我猜您正在正确读取第一个 XML,并且 XML 有一个您想要加载的 URL 列表,然后显示来自 TextFields 的一些信息。在不知道该 XML 的结构的情况下,我无法向您推荐任何工作代码,但我可以为您指明正确的方向。有关在 flash as3 上读取/迭代 XML 的更多信息,请阅读:http ://www.kirupa.com/developer/flashcs3/using_xml_as3_pg1.htm

//iterator var
var xml_read:uint=0;
//array of textfields for reference
var array_textFields:Array;

//config XML complete
function init(e:Event = null):void
{

  array_textFields = new Array();

  theXML = new XML(e.target.data);
  theXML.ignoreWhitespace = true;

  //this depends on the XML structure, please look at the article I linked
  //for more info on how to iterate an XML
  var i:uint=0;
  for(someUrl in theXML..url)
  {
    var fadedText:TextField = new TextField();

    //you should place each Textfield on different coord, otherwise
    //they will all stack on top of each other and you will only see one
    //for example:
    fadedText.y = (fadedText.height+10)*i;
    item.addChild(fadedText);
    array_textFields.push(fadedText);

    var distanceLoader:URLLoader = new URLLoader();
    distanceLoader.load(new URLRequest(someUrl));
    distanceLoader.addEventListener(Event.COMPLETE, distanceCalculated);

    i++;

  }
}
function distanceCalculated(e:Event):void
{

  var distanceXML:XML = new XML(e.target.data);
  distanceXML.ignoreWhitespace = true;

  //retrieve information
  var returnVar:String = (distanceXML.row.element.distance.text);

  //set to textfield
  TextField(array_textFields[xml_read]) = returnVar;

  //increase iterator
  xml_read++;
}

请记住,在 ActionScript3 中,所有网络 I/O 都是异步的。通常 EventListener 函数不会返回任何值,因为您不知道数据何时准备就绪。当异步调用 EventListener 函数时,您所做的是存储对您希望数据去往的位置(在您的情况下为 TextField 变量)的引用。

于 2012-07-24T18:08:24.967 回答