Mr_CryptoPrime
Mr_CryptoPrime

Reputation: 638

How to access root in AS3?

So after of hours of trying to figure out why my code isn't compiling, I come to find out that this code is incompatible for 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");

Anyone know how to access the root in AS3? Thanks!

Upvotes: 0

Views: 971

Answers (3)

parviz
parviz

Reputation: 1

Use MovieClip(root) instead of just _root, it works!
Like this:

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");

Upvotes: 0

Mr_CryptoPrime
Mr_CryptoPrime

Reputation: 638

Here is what I ended up doing:

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();
}

Upvotes: 1

Bart van Heukelom
Bart van Heukelom

Reputation: 44094

If you mean the root Sprite or MovieClip (document class) of the movie, you can just use the property root of any DisplayObject on the stage.

Upvotes: 0

Related Questions