0

I have created a class which loads the data from an xml file.

My gameObject.xml file:

<?xml version="1.0" encoding="utf-8" ?>
<xml>
    <player>
        <health>100</health>
        <speed>200</speed>
    </player>
    <enemy>
        <health>50</health>
    </enemy>
</xml>

The XmlDataLoader class loads data from the xml so other classes can access it. E.g. the Player accesses data from the player tag, Enemy from the enemy tag, and so on...

Thing is I don't know how to return the xml object only after the xml data has been loaded.

Whenever I trace m_xmlData outside this class, it always gives me a null. I have already initialized the XmlDataLoaderbefore passing it to the other classes.

Here's my XmlDataLoader class:

public class XmlDataLoader
{
   private var m_xmlData:XML = null;
   private var m_urlLoader:URLLoader = null;

   public function XmlDataLoader()
   {

   }

   public function initialize(sUrl:String = ""):void
   {
      if (!m_urlLoader)
      {
         if (sUrl == "")
         {
            m_urlLoader = new URLLoader(new URLRequest("gameObjectProperties.xml"));
         }
         else
         {
            m_urlLoader = new URLLoader(new URLRequest(sUrl));
         }

         if (!m_urlLoader.hasEventListener(Event.COMPLETE))
         {
            m_urlLoader.addEventListener(Event.COMPLETE, loadXml);
         }
      }
   }

   public function destroy():void
   {
      if (m_urlLoader)
      {
         if (m_urlLoader.hasEventListener(Event.COMPLETE))
         {
            m_urlLoader.removeEventListener(Event.COMPLETE, loadXml);
         }

         m_urlLoader = null;
      }

      if (m_xmlData)
      {
         m_xmlData = null;
      }
   }

   public function get xmlData():XML
   {
      return m_xmlData;
   }

   private function loadXml(e:Event):void
   {
      m_xmlData = new XML(e.target.data);

      if (m_urlLoader)
      {
         if (m_urlLoader.hasEventListener(Event.COMPLETE))
         {
            m_urlLoader.removeEventListener(Event.COMPLETE, loadXml);
         }

         m_urlLoader = null;
      }
   }

}

Thank you in advance for your help.

1 Answer 1

1

Its null because you are setting as null when you defining the var.
You did not show any code that shows how you are accessing the data.
So I will assume you are looking for the data before the load is completed

You should redispatch the event in the loadXml function

this.dispatchEvent( e );

// and out side the class you should have something like
var myDataLoader:XmlDataLoader = new XmlDataLoader()
    mydataLoader.addEventListener( Event.COMPLETE, gotData )

function gotData( e:Event ):void{
  trace( e.currentTarget.xmlData )
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.