AS3范围问题 [英] AS3 Scope Issue
问题描述
我有一个问题,范围在Flash中创建一个类。我创造,利用XML创建一个信息数组,我会处理和重新保存为XML的应用程序。
I am having an issue with scope in a class created in flash. I am creating an application that utilizes XML to create an array of information that I will manipulate and re-save to XML.
我有一个呼吁呼吁,我使用加载XML,将其转换成一个数组,并使用方法数组返回文档构造另一个类文档类。我已经成功地解析XML并创建数据的一个数组,但似乎无法通过loadXMLData类中的processXML函数的数据添加到阵列中。
I have a document class that is calling calling another class that I am using to load the XML, convert it into an array and return the array to the document constructor using a method. I have successfully parsed the XML and created an array of the data but can't seem to add data to the array via the processXML function inside the loadXMLData class.
我去掉了所有的东西从code无所谓。这里是什么,我试图做基本的重新presentation。
I stripped out all the stuff from the code that doesn't matter. Here is the basic representation of what i am trying to do.
我的文档类
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.*;
public class dashBoard extends MovieClip {
public var newData
//initialize the dashBoard
public function dashBoard(){
//construct the Dashboard Object
trace("Dashboard Initialized");
trace("|--------------XML Data--------------|");
newData = new loadXMLData();
trace(newData.getSections());
}
}
}
我的loadXMLData类
My loadXMLData Class
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.*;
public class loadXMLData extends MovieClip {
//initialize an array that will be used in the document class
public var sectionList:Array = new Array();
public function loadXMLData() {
//load the xml file containing the data
var myLoader = new URLLoader();
myLoader.load(new URLRequest("dashboard.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
//process the xml file after it loads
//and create an object
var newXML:XML = new XML(e.target.data);
//then I use the XML to populate my
//array i declared at the top
//this is a simple test
sectionList[0] = "test";
}
}
//and this is the method i use in the document class
//to get the sectionList array
public function getSections():Array{
return sectionList;
}
}
}
看来pretty的简单,但我似乎无法编辑阵列。该文档类总是返回一个空数组。任何想法?
It seems pretty straightforward but I can't seem to edit the array. The document class always returns a blank array. Any Ideas?
推荐答案
您尝试访问尚不exist.Dispatch事件在 loadXMLData.processXML()$信息C $ C>时,XML已经装载完毕,像这样的功能:
You're trying to access information that doesn't yet exist.Dispatch an event in your loadXMLData.processXML()
function when the XML has finished loading, like so:
function processXML(e:Event):void
{
//process the xml file after it loads
//and create an object
var newXML:XML = new XML(e.target.data);
dispatchEvent(new Event("xml_loaded"));
}
后来听吧,你做这样的事情之前:
Then listen for it before you do anything like this:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.*;
public class dashBoard extends MovieClip
{
public var newData:loadXMLData;
//initialize the dashBoard
public function dashBoard(){
//construct the Dashboard Object
trace("Dashboard Initialized");
newData = new loadXMLData();
newData.addEventListener("xml_loaded", _xmlLoaded);
}
private function _xmlLoaded(e:Event):void
{
trace("|--------------XML Data--------------|");
trace(newData.getSections());
newData.removeEventListener("xml_loaded", _xmlLoaded);
}
}
}
这篇关于AS3范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!