Link to home
Start Free TrialLog in
Avatar of jandhb
jandhb

asked on

xml node value

Here is the code that I currently have in my class...

-------------------------------

class WordOfDay extends MovieClip{

private var xmlPath:String;
private var target:MovieClip;
private var wordTxt:TextField;
private var success:Boolean;
private var xmlWord:XML;
private var xnRoot:XMLNode;
private var xnFirstItem:XMLNode;
private var xnWord:String;
private var xnWordValue:String;

//CONSTRUCTOR
function WordOfDay(target:MovieClip, oXmlPath:String){
xmlPath = oXmlPath;
loadXML(xmlPath);
newClip(target);
setCSS();
}

public function loadXML(xmlPath:String){
xmlWord = new XML();
xmlWord.ignoreWhite = true;
xmlWord.onLoad = function(success:Boolean):Void
{
if (success) {
xnRoot = this.firstChild;
xnFirstItem = xnRoot.firstChild;
xnWord = xnFirstItem.firstChild;
xnWordValue = xnWord.firstChild.nodeValue;
trace(xnWordValue);
} else {
trace ("no");
}
}
xmlWord.load(xmlPath);
}

public function newClip(target:MovieClip):Void {
this.createEmptyMovieClip("target", this.getNextHighestDepth());
target.createTextField("wordTxt", 3, 0, 0, 200, 12);
//PROPERTIES
target.wordTxt.border = true;
target.wordTxt.autoSize = "left";
target.wordTxt.multiline = false;
target.wordTxt.background = false;
target.wordTxt.html = true;
target.wordTxt.wordWrap = false;
target.wordTxt.embedFonts = true;
target.wordTxt.text = xnWordValue;
}

public function setCSS(){
var cssStyles:TextField.StyleSheet = new TextField.StyleSheet();
cssStyles.onLoad = function():Void {
target.wordTxt.styleSheet = this;
};
cssStyles.load("wod.css");
}
}

--------------------------------

My question is why when I trace out xnWordValue in loadXML() it shows the value in the xml file, but when I try to set xnWordValue to the text value of my textbox nothing displays?

Thank you.
Avatar of Proactivation
Proactivation
Flag of United Kingdom of Great Britain and Northern Ireland image

In classes, Flash can get confused about the scope of "this", but you can work around it with a pointer variable which I've called thisObj. Try this:

class WordOfDay extends MovieClip {
    private var xmlPath:String;
    private var target:MovieClip;
    private var wordTxt:TextField;
    private var success:Boolean;
    private var xmlWord:XML;
    private var xnRoot:XMLNode;
    private var xnFirstItem:XMLNode;
    private var xnWord:XMLNode;
    private var xnWordValue:String;
    private var response_xml:XML;
    //CONSTRUCTOR
    function WordOfDay(target:MovieClip, oXmlPath:String) {
        xmlPath = oXmlPath;
        loadXML(xmlPath);
        newClip(target);
        setCSS();
    }
    public function loadXML(xmlPath:String) {
        var thisObj:WordOfDay = this;
        xmlWord = new XML();
        xmlWord.ignoreWhite = true;
        xmlWord.onLoad = function(success:Boolean):Void  {
            if (success) {
                thisObj.response_xml = this;
                thisObj.loadedXML();
            } else {
                trace("no");
            }
        };
        xmlWord.load(xmlPath);
    }
    private function loadedXML():Void  {
        xnRoot = this.response_xml.firstChild;
        xnFirstItem = xnRoot.firstChild;
        xnWord = xnFirstItem.firstChild;
        xnWordValue = xnWord.firstChild.nodeValue;
        trace(xnWordValue);
    }
    public function newClip(target:MovieClip):Void {
        this.createEmptyMovieClip("target", this.getNextHighestDepth());
        target.createTextField("wordTxt", 3, 0, 0, 200, 12);
        //PROPERTIES
        target.wordTxt.border = true;
        target.wordTxt.autoSize = "left";
        target.wordTxt.multiline = false;
        target.wordTxt.background = false;
        target.wordTxt.html = true;
        target.wordTxt.wordWrap = false;
        target.wordTxt.embedFonts = true;
        target.wordTxt.text = xnWordValue;
    }
    public function setCSS() {
        var cssStyles:TextField.StyleSheet = new TextField.StyleSheet();
        cssStyles.onLoad = function():Void  {
            target.wordTxt.styleSheet = this;
        };
        cssStyles.load("wod.css");
    }
}
Avatar of jandhb
jandhb

ASKER

I tried what you did but it did not change the results. Still getting output in trace statement but nothing in the text box.
ASKER CERTIFIED SOLUTION
Avatar of BishopNeo
BishopNeo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also (forgot this the first time) instead of storing the object as Proactivation did in loadXML() you can also use the Delegate class.  Like so:

    public function loadXML(xmlPath:String) {
      var xmlDelegate = Delegate.create(this,loadedXML);
        xmlWord = new XML();
        xmlWord.ignoreWhite = true;
        xmlWord.onLoad = function(success:Boolean):Void  {
            if (success) {
                xmlDelegate(this);
            } else {
                trace("no");
            }
        };
        xmlWord.load(xmlPath);
    }

Then the first 2 lines of loadedXML would look like:
    private function loadedXML(wXML):Void  {
        xnRoot = wXML.firstChild;


Just place import mx.utils.Delegate; as the first line in the WordOfDay.as file, before *anything* else.