Search code examples
flashactionscript-3flash-builderflash-cs5flash-cs4

Actionscript 3 - How to keep the value from an event after removing that event


I set a value to my xml object (xml = new XML(e.currentTarget.data);) during my event handler function (the function is executed after the event.COMPLETE) and if I trace the object inside my event function handler it shows my xml data.

but if I try to trace it outside the event handler function it doesn't show my xml content. Isn't there a way to get my xml object content value to show in an other function but not in the event handler function?

private var xml:XML; 

public function XMLLoader(xmlURL:String) 
{ 
    var xmlURLRequest:URLRequest = new URLRequest(xmlURL); 
    var xmlURLLoader:URLLoader  = new URLLoader(xmlURLRequest); 
    xmlURLLoader.addEventListener(Event.COMPLETE, xmlData); 
}

private function xmlData(e:Event):void 
{
    e.currentTarget.removeEventListener(Event.COMPLETE, xmlData); 
    xml = new XML(e.currentTarget.data); 
    dispatchEvent(new Event(Event.COMPLETE)); 
    trace(xml); 
} 

public function getXMLData():void 
{ 
     //I've find out that this shows null because this function is faster
     //what do i do? put an event.complete in every following function?
    trace(xml);
}

Thanks.


Solution

  • //NOTE: USE THIS IF YOU WANT TO ACCESS DIRECTLY TO THE FUNCTIONS OF THIS "CLASS" WITHOUT USING ANY EVENT LISTENER ON THE CLASS CAUSE IT ALREADY HAS ITS EVENT LISTENER ON EACH FUNCTION (THIS CAUSE ONE FUNCTION ONLY getXMLData())

    private var xml:XML;

    public function XMLLoader(xmlURL:String) {

         var xmlURLRequest:URLRequest = new URLRequest(xmlURL); 
         var xmlURLLoader:URLLoader  = new URLLoader(xmlURLRequest); 
         xmlURLLoader.addEventListener(Event.COMPLETE, xmlData); 
    

    }

    private function xmlData(e:Event):void {

         e.currentTarget.removeEventListener(Event.COMPLETE, xmlData); 
         xml = new XML(e.currentTarget.data); 
         dispatchEvent(new Event(Event.COMPLETE)); 
         trace("1");//this used to come second of getXMLData() but it's solved now
         trace(xml); 
    

    }

    public function getXMLData():void {

         //This function was coming first so if you don't want to use an event listener outside    
         //this class to wait for event.complete you can use it here to wait for it and access
         //the function directly without being afraid of the object being null:
         addEventListener(Event.COMPLETE, go)
         function go(e:Event){
              trace("2"); //now it ONLY comes AFTER the event.complete, no need for external                   listeners over this class. declare the class object and use getXMLData() directly cause it always comes second the event handler xmlData() :)
              trace(xml);
         }
    }