10 Replies Latest reply on Jun 3, 2010 8:32 AM by logical_lunacy

    Creation Complete for Class Instance

    logical_lunacy

      I need to get a value from a class. The only problem is, since the class porcesses some xml, the value is returned before it is set.

       

      Is there any way to ensure that the new instance of my class is complete before I try to get a value from it?

        • 1. Re: Creation Complete for Class Instance
          UbuntuPenguin Level 4

          So do you want a completion event for when the class is done instantiating or when it is done processing xml ?

          • 2. Re: Creation Complete for Class Instance
            Rdsingh Ldh Level 1

            you can even dispatch event when the xml is loaded completely and then listent to that event and show the result.

            • 3. Re: Creation Complete for Class Instance
              logical_lunacy Level 1

              Rdsingh, would you be will to give an example of that?

              • 4. Re: Creation Complete for Class Instance
                Rdsingh Ldh Level 1

                hi just send me ur mail id and i will forward u the example once i done with that?


                • 5. Re: Creation Complete for Class Instance
                  Rdsingh Ldh Level 1

                  Hi I am sending you example of the event dispatcher

                   

                  In this example I have one mxml file which is application file and other is .as file which loads the xml. Once the xml is loaded then the event is dispatched and the same is listen in the mxml file.......

                   

                  Here is code for the both:

                   

                  1) this is code for mxml file:

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
                     
                      <mx:Script>
                          <![CDATA[
                             
                              import flash.events.Event
                             
                              private var objXMLLoader:XMLLoader
                              private function init()
                              {
                                  objXMLLoader = new XMLLoader();
                             
                                  objXMLLoader.addEventListener("xmlLoaded",done);   
                              }
                             
                             
                             
                              private function done(evt:Event)
                              {
                                 
                                  trace("loading done"+objXMLLoader.data);
                              }
                             
                          ]]>
                      </mx:Script>
                     
                     
                  </mx:WindowedApplication>

                  1 person found this helpful
                  • 6. Re: Creation Complete for Class Instance
                    Rdsingh Ldh Level 1

                    2) this is code for the .as file:

                     

                    package
                    {
                        import flash.events.Event;
                        import flash.events.EventDispatcher;
                        import flash.net.URLLoader;
                        import flash.net.URLRequest;
                        import flash.xml.*;
                       
                        public class XMLLoader extends EventDispatcher
                        {
                           
                            private var loader:URLLoader
                            private var url:String = "test.xml";
                           
                            public var data:XML;
                           
                            public function XMLLoader()
                            {
                                loader = new URLLoader();
                                loader.addEventListener(Event.COMPLETE, xmlLoaded);
                                loader.load(new URLRequest(url));   
                            }
                           
                            private function xmlLoaded(evt:Event):void
                            {
                                //trace(evt.target.data);       
                               
                                data = new XML(evt.target.data);
                                dispatchEvent(new Event("xmlLoaded"));   
                               
                               
                            }

                     

                        }
                    }

                     

                    I hope this will give u an idea of what u needed..........

                     

                     

                    and still if u persist some problem feel free to ask me?

                    1 person found this helpful
                    • 7. Re: Creation Complete for Class Instance
                      logical_lunacy Level 1

                      Hey Rdsingh,

                       

                      You've been a great but this still doesn't quite solve my problem. I hope you can tolerate me bothering you once again.

                       

                      The xml file, once loaded, has to be processed a certian way. As there are several xml files, I have a class called LoadResources.

                       

                      package

                       

                       

                       

                       

                       

                      com.ascentify.components.content.functions

                      {

                       

                      import com.ascentify.Main;

                       

                      import com.ascentify.components.content.functions.LoadZipFile;

                       

                      import com.ascentify.components.text.variables.TimingVariables;

                       

                      import com.ascentify.components.video.variables.VideoVariables;

                       

                       

                      import flash.events.Event;

                       

                      import flash.net.URLLoader;

                       

                      import flash.net.URLRequest;

                       

                      import flash.utils.ByteArray;

                       

                       

                      import mx.controls.Alert;

                       

                      import mx.events.FlexEvent;

                       

                       

                      public class LoadResources

                      {

                       

                      private var _sampleFolder:String;

                       

                      private var _zipId:String;

                       

                       

                      public var arrIntervals:Array;

                       

                      private var xmlLoader:URLLoader;

                       

                       

                      public var _vidSource:String;

                       

                       

                      public static var videoVar:VideoVariables;

                       

                      public static var timeVar:TimingVariables;

                       

                       

                      public function LoadResources()

                      {

                       

                       

                      videoVar =

                      new VideoVariables();

                      timeVar =

                      new TimingVariables();

                       

                      onLoadResources();

                       

                       

                      }

                       

                       

                       

                      public function onLoadResources():void

                      {

                       

                       

                      xmlLoader =

                      new URLLoader();

                      xmlLoader.addEventListener(Event.COMPLETE, onXmlComplete);

                      xmlLoader.load(

                      new URLRequest("resources.xml"));

                      }

                       

                      //somewhere between onLoadResources and onXMLComplete, the _vidSource dies.

                       

                       

                      public function onXmlComplete(e:Event):void

                      {

                       

                       

                       

                      var _xml:XML = new XML(xmlLoader.data);

                       

                      onTimingLoaded(_xml);

                      }

                       

                       

                      public function onTimingLoaded(xml:XML):void

                      {

                       

                       

                       

                      //Obtain subset of the original XML

                       

                       

                      var _timings:XMLList = new XMLList(xml..*);

                       

                       

                      //Intervals Collection

                      arrIntervals =

                       

                      new Array();

                       

                       

                      //Local vars

                       

                       

                      var counter:int = 0;

                       

                       

                      var _start:int = 0;

                       

                      var _end:int = 0;

                       

                      var _offset:int = 1; //Milisecond offset

                       

                       

                      var _duration:int = 0;

                       

                       

                      //Extract Timings

                       

                       

                      for each(var _timing:XML in _timings)

                      {

                       

                      for each(var item:XML in _timing.children())

                      {

                       

                      if(item.localName() == "w")

                      {

                       

                      // trace("**");

                       

                       

                      if(counter == 0)

                      {

                      _start = item.attribute(

                      "start");

                       

                      // trace("_start=[" + _end + "]+[" + _offset +"]=" + _start);

                      _end = item.attribute(

                       

                      "duration");

                       

                      // trace("_end=[" + _start + "]+[" + item.attribute("duration") + "]=" + _end);

                      _duration = item.attribute(

                       

                      "duration");

                      }

                       

                      else

                      {

                      _start = _end + _offset;

                      _end = _start + int(item.attribute(

                       

                      "duration") - _offset);

                      _duration = item.attribute(

                      "duration");

                       

                      }

                      arrIntervals[counter] = [

                      _start,_end,_duration]

                      counter += 1;

                      }

                       

                      else if(item.localName() == "f")

                      {

                      _vidSource =

                       

                      "8C6F1836C10DCEED0FD08135C6D09444/story/resources/" + item.attribute("name");

                       

                      }

                      }

                      }

                      timeVar.arrayIntervals = arrIntervals;

                      }

                       

                       

                       

                       

                      public function getVideoSource():String

                      {

                       

                      //return "data/sri/data/8C6F1836C10DCEED0FD08135C6D09444/story/resources/Franklin Wants a Pet2.flv";

                       

                       

                      return _vidSource;

                      }

                      }

                      }

                       

                      The problem is I have another class, VideoMain, which needs the value of _vidSource. However, VideoMain creates the instance of LoadResources and gets the value of _vidSource before LoadResources has finished loading the xml, hence, _vidSource is still null. Any ideas?

                      • 8. Re: Creation Complete for Class Instance
                        pauland Level 4

                        Check out Events - for asynchronous operations such as these you should be dispatching an Event when the data is ready.

                         

                        Google is your friend.

                         

                        Paul

                        • 9. Re: Creation Complete for Class Instance
                          Rdsingh Ldh Level 1

                          you can do one thing, dont create a new instance of videoComponent.

                           

                          Once the xml is loaded then dispatch the event (as I posted the example for the event dispatcher), and then listening to that evnet in

                           

                          class, create a new instance of the  videoComponent.

                           

                          This will solve ur problem

                          • 10. Re: Creation Complete for Class Instance
                            logical_lunacy Level 1

                            Hey Rdsingh. I've being trying all week to work with what you told me, but I still don't think you get exactly what I mean. I'm going to try an explain again, because I'm sure I just Haven't explained myself properly.

                             

                            I have a Video Module. This module works perfectly, expect that I want to pass it the video source value from another class. This class is called LoadResources.as.

                             

                            The issue is the LoadResources class loads an xml file. If I set the variable I wish to pass before the loader has completed, there is no problem. However, the variable value is inside the xml file, and therefore must be set after the xmlLoaded event.

                             

                            LoadResources.as currenlt does not extend anything. Is there any way I can create an instance of LoadResources.as in my Video Module class, and wait untilthe instances is fully created before trying to get the value from it, like the example below?

                             

                            public function Video()
                            {

                                 _loadResources = new LoadResources();

                                 _loadResources.addEventListener(Event.COMPLETE, initVideo);

                            }

                             

                            public function initVideo():void
                            {       
                                 _displayTimer = new Timer(10);
                                  _displayTimer.addEventListener(TimerEvent.TIMER, handleOnUpdateDisplay);
                                               
                                 vidComponent.netStream.play(_loadResources.getVideoSource());
                                 vidComponent.netStream.pause();
                                 vidComponent.netStream.client = this;
                                       
                            }