Solved

xml node value

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Flash media server 1 332
How to disable  HTML5 video in chrome 2 1,551
Adobe flash player fails to install 12 307
video live streaming 1 123
This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
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…
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.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

773 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