Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

xml node value

Posted on 2006-07-04
7
Medium Priority
?
168 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 100 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

636 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