如何在AS3中访问root?

时间:2011-04-30 08:46:02

标签: xml flash actionscript-3

因此,经过几个小时的努力弄清楚为什么我的代码没有编译,我发现这个代码与AS3不兼容:

function loadXML(loaded) {
if (loaded) {
_root.inventor = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;
_root.comments = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue;
name_txt.text = _root.inventor;
comment_txt.text = _root.comments;
} else {
  trace("file not loaded!");
}
}

xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("http://localhost/connect.php");

任何人都知道如何访问AS3中的根目录?谢谢!

3 个答案:

答案 0 :(得分:1)

这是我最终做的事情:

var myXML:XML = new XML();
var XML_URL:String = "http://localhost/connect.php";
var myXMLURL:URLRequest = new URLRequest(XML_URL);
var myLoader:URLLoader = new URLLoader(myXMLURL);
myLoader.addEventListener("complete", xmlLoaded);

function xmlLoaded(event:Event):void
{
    var finalXML:XML = XML(myLoader.data);
    //trace(finalXML);
    trace("Data loaded.");
    //trace(finalXML.triviaQuestion[0].answer1[0]);
    //questionBox.text = (finalXML.triviaQuestion[0].question[0]).toString();
}

答案 1 :(得分:0)

如果您指的是电影的根SpriteMovieClip(文档类),则可以使用舞台上任何root的属性DisplayObject。< / p>

答案 2 :(得分:0)

使用MovieClip(root)代替_root,它有效! 像这样:

function loadXML(loaded){
 if (loaded) {
  MovieClip(root).inventor = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;
  MovieClip(root).comments = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue;
  name_txt.text = MovieClip(root).inventor;
  comment_txt.text = MovieClip(root).comments;
 } else {
  trace("file not loaded!");
 }
}

xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("http://localhost/connect.php");