Solved

xml node value

Posted on 2006-07-04
7
162 Views
Last Modified: 2010-04-03
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.
0
Comment
Question by:jandhb
  • 2
7 Comments
 
LVL 12

Expert Comment

by:Proactivation
ID: 17037156
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");
    }
}
0
 
LVL 1

Author Comment

by:jandhb
ID: 17039087
I tried what you did but it did not change the results. Still getting output in trace statement but nothing in the text box.
0
 
LVL 3

Accepted Solution

by:
BishopNeo earned 25 total points
ID: 17075047
Several things were amiss in this Class.  One of which is hard to track because of Flash's limited error management, another of which is the bane of my existance.  First, the embedFonts attribure of the textfield will cause you to get a blank field unless you set a format and assign a font. The catch is you have to create a new font inthe library and link that one.  Something like:  
            var wordFmt:TextFormat = new TextFormat();
            wordFmt.font = "MyNewFont";
            target.wordTxt.text = xnWordValue;
            target.wordTxt.setTextFormat(wordFmt);        

Otherwise, comment out embedFonts.

Second:  change your constructor to this:
    //CONSTRUCTOR
    function WordOfDay(target_mc:MovieClip, oXmlPath:String) {
        xmlPath = oXmlPath;
        loadXML(xmlPath);
      target = target_mc;
        //newClip(target);
        setCSS();
    }

Two things there.  By changing the incoming variable name and *then* setting it to the local (Class) variable target your telling the class what clip to assign everything to.  Before, it got confused between incoming and local.  Basically it though you were passing in the contents of the target Class variable, which is blank.  Now, why the newClip() function is commented out... :)

*This* is the bane I mentioned earlier.  What most people don't realize is that Actionscript is asyncronous.  And by that I mean it will fire off the loadXML() function and immediately fire the newClip() function.  It doesnt wait for the loadXML() function to return before going to the next line in the program.  Basically there is no flow control on AS.  To get around this I moved the newClip() call to the end of loadedXML().  Once I did that it worked fine.  Here is the complete source:


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_mc:MovieClip, oXmlPath:String) {
        xmlPath = oXmlPath;
        loadXML(xmlPath);
            target = target_mc;
        //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);
            newClip(target);
    }
        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");
    }
      

}


Good luck!
0
 
LVL 3

Expert Comment

by:BishopNeo
ID: 17075099
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.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now