You can’t add event listeners to an object that is returned from a method and expect to receive the events that are dispatched within the same method that returns the object to which you are adding event listeners.
This on is perhaps not so obviously stated, but can be made more clear via some code (hopefully):
-
-
-
myObject:MyObject = new Object(fooParam);
-
myObject.addEventListener("myEventType", myCallBackFunction);
-
-
//where MyObject Class looks like this:
-
-
public class MyObject extends EventDispatcher
-
{
-
// constructor
-
public function MyObject (fooParam:String, target:IEventDispatcher=null)
-
{
-
super(target);
-
// do some stuff and then dispatch event
-
dispatchEvent(new MyEvent(MyEvent.MY_EVENT_TYPE));
-
}
-
This just won’t work.
Better to do something like:
-
-
-
myObject:MyObject = new Object();
-
myObject.addEventListener("myEventType", myCallBackFunction);
-
myObject.doStuff(fooParam);
-
-
//where MyObject Class looks like this:
-
-
public class MyObject extends EventDispatcher
-
{
-
// constructor
-
public function MyObject (target:IEventDispatcher=null)
-
{
-
super(target);
-
}
-
-
public function doStuff(fooParam:String):void {
-
// do some stuff and then dispatch event
-
dispatchEvent(new MyEvent(MyEvent.MY_EVENT_TYPE));
-
}
-
That should work and not cause you to chase your tail trying to figure out why things are not getting dispatched or not being responded to by an eventListener
Image may be NSFW.
Clik here to view.