views:

344

answers:

3

Hello, what would be the simpliest way to communicate between independent classes? I have been searching for two days and couldn't find nothing, isn't there a way that one class has a dispatchEvent and the other an addEventListener, with a custom Event?? I could only find solutions within the same class o with a parentchild relationship, but what I'm looking for is like a "brother" relationship so to speak thanks

A: 

So if you have ChildClass1, ChildClass2, and ParentClass, where ChildClass1 and ChildClass2 are both the children of ParenClass.

ChildClass1 will dispatch an event. ParentClass will listen for this event, and then it's handler will update ChildClass2.

quoo
A: 

If you don't have a ParentClass, you could also use a ChildManagerClass that registers Childs and notifies them accordingly.

Cay
could you explain that a bit more with an example?? thanks
+1  A: 

In general, you want the class that dispatches the event to either extend EventDispatcher or implement IEventDispatcher. (All DisplayObjects do, so if your classes are DisplayObjects, you don't need to do anything extra.)

In the dispatching class:

class DispatchingClass extends Event Dispatcher {
    function doSomething() {
        // do stuff
        dispatchEvent(new Event("FOO"));
    }
}

In the listening class:

class ListeningClass {
    function startListening(dispatcher:DispatchingClass) {
        dispatcher.addEventListener("FOO", handleFoo);
    }

    function handleFoo(evt:Event) {
        // do stuff
    }
}

EventDispatchers work fine with custom events.


If for some reason your listening class doesn't have and can't get an instance of your dispatching class, you can make a global event broadcaster. Basically, make a universally-accessible class that extends EventDispatcher (or implements IEventDispatcher) and listens for and dispatches events to anything that tells it to.

Here is a bare-bones implementation of an event broadcaster:

import flash.events.EventDispatcher;

public class EventBroadcaster extends EventDispatcher {
 private static var _instance:EventBroadcaster = new EventBroadcaster();

 public function EventBroadcaster() {
  if (_instance != null) {
   trace ("Error: an instance of EventBroadcaster() already exists.");
  }
 }


 public static function getInstance():EventBroadcaster {
  return EventBroadcaster._instance;
 }
}

You use it pretty much the same way:

class DispatchingClass {

 function doSomething() {
  // do something
  EventBroadcaster.getInstance().dispatchEvent(new Event("FOO"));
 } 
}

class ListeningClass {
 function startListening() {
  EventBroadcaster.getInstance().addEventListener("FOO", handleFoo); 
 }

 function handleFoo(evt:Event) {
  // do stuff
 }
}

dispatchEvent() and addEventListener() are just the functions from the built-in EventDispatcher.

There's some discussion at Event Broadcaster - Simple events solution... on ways to make an event broadcaster and how to add useful features. The article Centralized Event Management in Actionscript 2.0 has a good introduction to the concept.

Selene