views:

124

answers:

2

I'm trying to a make a simple class in an actionscript file that handles the reading in and parsing/managing of an xml file. This class takes a movieclip as a parameter and has the movie clip act according to the results of the import. I've tried this:

class FileReader {
    var menu:MovieClip;
    function FileReader(newMenu) {
     menu = newMenu;
    }
    //Load the specified xml file
    function loadFile(fileName) {
     menu.gotoAndStop("loading");
     var name = levelName+".xml";
     var xmlFile = new XML();
     xmlFile.ignoreWhite = true;
     xmlFile.load(name);
     xmlFile.onLoad = function() {
      //Parse Input
      menu.gotoAndStop("loaded");
     };
    }
}

For some reason, when the code reaches the onLoad function, the file loads properly but the application has no more knowledge of the existence of the menu movieclip. If I try to trace any attributes of the menu, it says it is undefined. So then I tried this:

class FileReader {
    var menu:MovieClip;
    var xmlFile:XML;
    function FileReader(newMenu) {
     menu = newMenu;
    }
    //Load the specified xml file
    function loadFile(fileName) {
     menu.gotoAndStop("loading");
     var name = fileName+".xml";
     xmlFile = new XML();
     xmlFile.ignoreWhite = true;
     xmlFile.load(name);
     xmlFile.onLoad = function() {
      //Parse Input
      menu.gotoAndStop("loaded");
     };
    }
}

In this case, the xml file won't load at all, and the xmlFile object is undefined. What's going on here and why are neither of these approaches working?

A: 

By using the first approach, I found that I could simply pass the movie clip as a parameter. The function would then recognize the movie clip and act properly as expected. Still confused as to why it won't work without the parameter passing though.

EDIT: I guess that actually didn't work as I had thought. I'm still trying though! Anyone with other thoughts?

Everett
A: 

This is kind of silly, but I've finally found a way to make this work:

class FileReader {
    var menu:MovieClip;
    function FileReader(newMenu) {
        menu = newMenu;
    }
    //Load the specified xml file
    function loadFile(fileName) {
        menu.gotoAndStop("loading");
        var newMenu:MovieClip = menu;   //Make a refernce to menu here
        var name = levelName+".xml";
        var xmlFile = new XML();
        xmlFile.ignoreWhite = true;
        xmlFile.load(name);
        xmlFile.onLoad = function() {
                //Parse Input
                newMenu.gotoAndStop("loaded"); //Call the refence rather than the actual object
        };
    }
}

By making a new reference to the menu, the onLoad function can use this reference to talk to the actual menu movie clip. I guess that works.

Everett