PDA

View Full Version : EventDispatcher Problems



trundrumbalind
August 11th, 2008, 06:00 AM
Hi there, I'm having a strange problem dispatching custom events across different classes. Here's some basic code...

MyEvent.as (extends Event)
public function MyEvent(message : String) {
this.message = message;
}

ClassA.as (extends Sprite)
public function ClassA() {
addEventListener(MyEvent.ACTION, listenerFunction);
}

public function listenerFunction(e : MyEvent) : void {
trace("HEARD");
}

ClassB.as (extends Sprite)
public function ClassB() {
dispatchEvent(new MyEvent("MY MESSAGE"));
}


Now if the event is dispatched in the same class as the event listener is defined, it hears it ok. But the example above doesn't work! Any ideas guys?

Thanks a lot,
Dave

senocular
August 11th, 2008, 07:37 AM
Event's aren't heard everywhere. If you associate a CLICK event listener with a play button, it will not fire when a stop button is clicked. Same thing applies to your classes A and B. You're "clicking" B in your code - its the class sending out an event. Your listener, however, is defined with A. That listener will only be called when the event occurs in class A. If you need it to respond to B's event, then you need to listen for that event in an instance of B

public function ClassA() {
instanceOfB.addEventListener(MyEvent.ACTION, listenerFunction);
}

The only other way that event listener would work otherwise is if B was a display object child of A and MyEvent was dispatched from B with bubbles set to true

dispatchEvent(new MyEvent("MY MESSAGE", true));

Also, I know you've posted pseudo code up there, but be careful about dispatching events in constructors, only in rare situations (timeline based display objects bubbling events is the only one I can think of) can a constrctor-based event actually be heard by anyone since no one would have a chance to add a listener to it.

var b = new B(); // constructor code (includes dispatch);
b.addEventListener("type", listener); // whoops! too late!

trundrumbalind
August 11th, 2008, 07:43 AM
Thanks for your reply sen.
Ok, I think I get the point now. However, it just seems really limiting to have to tell listeners where to expect the event to be dispatched from.

What happens if my event might be dispatched from more than one class? Does that mean I will have to "addEventListener" for each and every class that might dispatch it?

senocular
August 11th, 2008, 07:56 AM
What happens if my event might be dispatched from more than one class? Does that mean I will have to "addEventListener" for each and every class that might dispatch it?

Pretty much, yeah. Though you can create manager classes that consolidate them. Such a class would handle all the necessary listening for your individual dispatching classes and redispatch them from itself. That way other classes would only need to listen to the manager class.

A lot of this depends on application/class design. When you take a modular approach, usually there's one or so class which represents the core functional component of what you're trying to achieve. That class would be the receiver of events from its internal parts and the dispatcher of events for other classes or systems that implement it.

trundrumbalind
August 11th, 2008, 08:01 AM
Yeah good point. Very interesting topic though and was great to have your insight into things Sen. The idea of an EventManager is great. Will definately get into building one soon.

As always, thank you very much for taking the time to help me out. Really appreciate it ;)