Solved

xml node value

Posted on 2006-07-04
7
164 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

679 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