Skip to main content
Participating Frequently
June 11, 2011
Question

Problem in accessing an external class in the timeline script

  • June 11, 2011
  • 3 replies
  • 1234 views

Hi Everyone....

           I'm new to AS3 and I'm trying to load a data from a XML file("AppData") and set the data(text) in a dynamic text field("Label") on the stage....  For accessing and manipulating the XML file ,I have an external as file with a class called "DataClass". And I running scripts in project frame to access the data from XML in the class "DataClass" to set it in the text field

This topic has been closed for replies.

3 replies

Participating Frequently
June 11, 2011

Hi... Problem solved... That was due to the XML loader...

I just added timer event after the object created .. when the timer complete I access the data and set to the text field... now it works...

But I have another question...I want to share this same data in two different timeline scripts from two different scenes ...

can I use the class member variable as static ( is it supported in AS?)  or

can I use single object of the class in those two scripts...

Thank you....

Participating Frequently
June 11, 2011

Sorry Accidently hit Enter button and the question is saved incomplete....

I'm trying to load a data from a XML file("AppData") and set the data(text) in a dynamic text field("Label") on the stage....  For accessing and manipulating the XML file ,I have an external as file with a class called "DataClass". And I running scripts in project frame to access the data from XML in the class "DataClass" to set it in the text field.                                                                                                                                                             Here is the script.......                                                                                                                                                                                                     "AppData"                                                                                                                                                                                                          

<demo>
<Value>10</Value>
</demo>

                                                                                                                                                                                                                                              "DataClass"

package  {
          import flash.display.MovieClip;

     import flash.events.Event;
     import flash.xml.XMLNode;
     import flash.xml.XMLDocument;
     import flash.net.URLLoader;
     import flash.net.URLRequest;
     
     import flash.xml.XMLDocument;
     import flash.xml.XMLNode;
     
     public  class DataClass extends MovieClip {
          
     public  var value:Number;
 
          
          public var urlVar:String = "AppData.xml";
          public static var XMLVALUESLOADED:String="xml_value_loaded";
          
          public var DataLoaded:Number=0;

          public  function DataClass() {
               // constructor code
               var urlLdr:URLLoader = new URLLoader();
               var urlRqst:URLRequest = new URLRequest (urlVar);
               
               urlLdr.load(urlRqst);
               urlLdr.addEventListener(Event.COMPLETE, sortData);
     }
     
          
          function sortData(e:Event)
          {
               var mainNode:XMLDocument= new XMLDocument ();
               mainNode.ignoreWhite=true;
               mainNode.parseXML(e.target.data);               
               
               DataLoaded=1;
               
               var fc;
               var sc;
                     fc = mainNode.firstChild;
               
               while (fc!=null)
               {
                    trace( "fc .node ",fc.nodeName );
                    sc=fc.firstChild;
                    while (sc!=null)
                    {
               
                    if(sc.nodeName=="Value")
                         {
                         value=sc.firstChild.nodeValue;     
                         trace(value);
                    }
                                         sc=sc.nextSibling;
                    }
                    fc=fc.nextSibling;
               }
               dispatchEvent(new Event(DataClass.XMLVALUESLOADED));
          }
          
     public function get():Number
          {
          trace("Hello " + value);
          return value;
     }

}
}

"TimeLine Script "

import flash.system.fscommand;
import flash.display.Stage;
import DataClass;


var Data:DataClass=new DataClass();

gotoAndStop(1);

     trace("Hello");
     var Value:Number;
  Value=Data.get();
trace(Data.get());
Label.text=Value.toString(10);

So By the TimeLine script, An Object is created so the variables are initialized and then accessed and set as the value to the text field in the scene .. right?   But The function get() return NaN,... i.e Either the constructor is not finished or the XML file is not loaded yet... how to solve this problem..

Inspiring
June 11, 2011

You need to listen to the moment when XML loaded - loading is an asynchronous event. So, you Data class should be (it doesn't have to extends MovieClip but rather be an EventDispatcher:

package  {
     import flash.events.Event;
     import flash.events.EventDispatcher;
     import flash.xml.XMLNode;
     import flash.xml.XMLDocument;
     import flash.net.URLLoader;
     import flash.net.URLRequest;
     
     public class DataClass extends EventDispatcher {
          public var value:Number;
          public var urlVar:String = "AppData.xml";
          public static var XMLVALUESLOADED:String = "xml_value_loaded";
          public var DataLoaded:Number = 0;
          
          public  function DataClass() {

          }
          
          public function init():void {
               var urlLdr:URLLoader = new URLLoader();
               var urlRqst:URLRequest = new URLRequest (urlVar);
               urlLdr.load(urlRqst);
               urlLdr.addEventListener(Event.COMPLETE, sortData);
          }

          private function sortData(e:Event) {
               var mainNode:XMLDocument = new XMLDocument ();
               mainNode.ignoreWhite = true;
               mainNode.parseXML(e.target.data);     
               DataLoaded = 1;
               var fc = mainNode.firstChild;;
               var sc;
               while (fc)
               {
                    trace( "fc.node ",fc.nodeName );
                    sc = fc.firstChild;
                    while(sc)
                    {
                         if (sc.nodeName == "Value")
                         {
                              value=sc.firstChild.nodeValue;    &nbsp ;
                              trace(value);
                         }
                         sc = sc.nextSibling;
                    }
                    fc = fc.nextSibling;
               }
               dispatchEvent(new Event(DataClass.XMLVALUESLOADED));
          }
          
          public function get():Number {
               trace("Hello " + value);
               return value;
          }
     }
}

Then on the timeline:

var Value:Number;
var Data:DataClass = new DataClass();
Data.addEventListener(DataClass.XMLVALUESLOADED, onXMLLoad);
// inititate here
Data.init();

function onXMLLoad(e:Event):void {
     Value = Data.get();
     trace(Value);
     Label.text = Value.toString(10);
}

While you are on it - you should consider sticking to naming conventions. Variables with upper cases indicate classes - so I suggest you rename them with camel cases.

Participating Frequently
June 11, 2011

Thanks Andrei1....   I have another question about sharing a member variable of the class in two different timeline scripts of two different scenes..... Like I need to display the same value in two different labels in two different scenes... How to do that?

Inspiring
June 11, 2011

What's the problem?