views:

76

answers:

2

Ok here is what I am currently trying to do. I have a class called vdata.as which takes 2 paramaters both are strings sent from the main stage. Parameter one is the location for an XML file that I need to open and read. The second parameter is the name of the video I am currently looking for.

Now I can get the data from the XML file and display it with out any issue if its called from my class but when I try to access any of it from the stage I get undefined.

import flash.net.*;
import flash.display.*;
import flash.events.*;

public class videoData
    {
      private var mName:String;
  private var mLink:String;
  private var mCategory:String;
      public static var elementArray:Array;

      // Constructor
  public function videoData(xmlPath:String,xmlVidSrc:String,pMC:MovieClip)
  {
        pXmlPath = xmlPath;
 pXmlVidSrc = xmlVidSrc;
 xmlloader = new URLLoader();
            elementArray = new Array();
  }

      public function getXML()
  {
 XMLData();
      }

      private function XMLData()
  {
        xmlloader.load(new URLRequest(pXmlPath));
 xmlloader.addEventListener(Event.COMPLETE,parseXMLData);

      } 

      private function parseXMLData():void
  {
 var x:XML = new XML(xmlloader.data);
        Init(x); 
  }

      private function Init(m:XML):*
  {
        var i:Number;
 for(i=0; i<m.videos.videoname.length(); i++)
 {
     if(m.videos.videoname[i].@name == pXmlVidSrc)
     {
  videoData.elementArray.push(m.videos.videoname[i].@name);
  videoData.elementArray.push(m.videos.videoname[i].@category);
  videoData.elementArray.push(m.videos.videoname[i].link.@url);
                 }
 }
}

}

When I call it from the main stage the code is as follows.

var xData:videoData = new videoData(xmlPath,vidSrc,this); xData.getXML();

then when I try to access any elements of videoData.elementArray they come up undefined... Im just smacking my head on my desk trying to figure this out any help would be great.

A: 

Why is elementArray a static var, you only need to make it public to use it outside the function.

I'm quite confusing but you may want to try a debugging tool like "De MonsterDebugger", I would start by tracing xmlloader.data in the parseXMLData function.

Dennis
basically what its suppose to do is get the XML data dump it to an array and return it to the main stage so I can use it else were in the app. But currently its not working. Like I said I can see the data while in the class but unable to view it in the main stage. Actually I can't even see it after the addeventlistener fires. Ill check the debuger and see what it comes up with.
Ian
A: 

"addEventListener" doesn't "fire"...the event does. You'll need to add a boolean to state for the stage that elementArray has been populated and set that after the init function.

Is elementArray something that needs to be true across all instances of videoData? If not, it shouldn't be static. You can use MovieClip(this.root).xData to access that instance of the video class from one of your other classes.

If the event has completed and the array is still empty - then it wasn't populated by your parser. You can also do checks to see if the elementArray.length > 0.

EDIT in response to comment:

as a public member or preferably a read-only property make a boolean variable:

var parseComplete:Boolean;

Set it to false in your constructor.

Then, after your call to "Init" in your Event.COMPLETE callback set:

parseComplete=true;

Then make sure parseComplete == true before you ever access elementArray. If you're waiting for the parser to complete you might want to set a timeout or some sort of try/catch mechanism just in case there are any unforeseen errors that would cause some sort of:

while( !xData.parseComplete ) { }

To loop indefinitely. It all depends on the usage. Personally I'd probably add a callback from the event listener to the stage to trigger whatever is supposed to happen next.

McAden
Thank you. Sorry to be a pain but could I get an example of what you mean by "add a boolean to state for the stage" I think I have an idea but just want to make sure. And no elementArray does not need to be true I just removed the static from that.
Ian
thank you greatly appreciated.
Ian
did that do the trick?
McAden
yes it did thank you.. Sorry for the delay.
Ian