views:

51

answers:

3

Hi there, I have instantiated a class (class1) that loads some data via PHP in an AS3 Flex project. I instantiate class1, which is asynchronous and utilizes a URLLoader() to obtain its data.

Then I would like to instantiate class2, which needs to USE the data from class1, but when I simply instantiate it I find that class1's instance variable is not populated yet because I am calling class2 too quickly.

How can I create such a dependency that will link correctly? I am really new to using listeners, but I am imagining a listener in class2 might be what I need? Or maybe I can pass a reference to the listener in class1 into class2? I also know nothing about dispatching events... is this the key?

Here's an example in pseudo code:

var class1:myC1 = new myC1("http://some/url/to/utilize");
//this never shows up because it hasn't loaded at the time i request it
trace("the stuff from class1 is: ", class1.myXMLList);
//and of course i can't use it here either
var class2:myC2 = new myC2(0x000000, class1.myXMLList);

Thanks for any help, jml

+2  A: 

Hi jml,

Based on your question, I'm posting this "pseudo code" hopping it will help

package {

  import flash.events.Event;
  import flash.events.EventDispatcher;
  import flash.net.*;

  public class Class1 extends EventDispatcher {
 private var _loader:URLLoader;

 public var data:XMLList;

 public function Class1(url:String) {
   _loader = new URLLoader();
   _loader.addEventListener(Event.COMPLETE, completeHandler);
   // rest of the code here ....

   _loader.load(new URLRequest(url));
 }

 protected function completeHandler(event:Event):void {
   data = new XMLList(_loader.data); // probably wrong but
          // I figured you had already
          // implemented this part

   dispatchEvent(new Event(Event.COMPLETE));
 }
  }
}

I figured class 2 might look like this

package {

  public class Class2 {

 // your stuff here

 public function Class2(color:uint, data:XMLList) {

 }
  }

}

The main class

package {

  import flash.display.Sprite;
  import flash.events.Event;

  public class Main extends Sprite {

 public var c1:Class1;
 public var c2:Class2;

 public function Main() {
   c1 = new Class1("http://some/url/to/utilize");
   c1.addEventListener(Event.COMPLETE, completeHandler);
 }

 protected function completeHandler(event:Event):void {
   c2 = new Class2(0xffcc00, c1.data);
 }
  }

}

Hope this helps

just_a_dude
cool. thanks for the tips. i'll check it out and post back if i have any problems. much appreciated!btw- do you have any links regarding learning about event dispatching?
jml
A: 

@just_a_dude: ok i have another question:

public function Main() 
{ 
    c1 = new Class1("some/url/to/utilize";); 
    c1.addEventListener(Event.COMPLETE, completeHandler); 
}

i thought that this would autocomplete to Event.COMPLETE but it's not... should i look for Event.ADDED ? why or why not? – jml 3 hours ago [delete this comment]

jml
+1  A: 

Hi again jml,

If you want Flex/FlashBuilder to autocomplete the events for your classes, you can use the "Event" metadata tag before your class definition. Based on the example above, it would look like this

package {

  import flash.events.Event;
  import flash.events.EventDispatcher;
  import flash.net.*;

  // use the "Event" metadata tag 

  [Event(name="complete", type="flash.events.Event")]

  public class Class1 extends EventDispatcher {

  private var _loader:URLLoader;

  public var data:XMLList;

  public function Class1(url:String) {
    _loader = new URLLoader();
    _loader.addEventListener(Event.COMPLETE, completeHandler);
    // rest of the code here ....

    _loader.load(new URLRequest(url));
  }

  protected function completeHandler(event:Event):void {
    data = new XMLList(_loader.data); 
    dispatchEvent(new Event(Event.COMPLETE));
  }
  }
}

Now FlexBuilder/FlashBuilder should "autocomplete" the events for that class. The downside is that you can't use "constants" with the "Event" metadata tag so this :

[Event(name=Event.COMPLETE, type="flash.events.Event")]

wouldn't work.

You can read more about event/event dispatching here.

Hope that helps :)

just_a_dude
Yes this definitely helps. Thanks very much.
jml