I need an event redispatcher at the top of the DisplayObject
hierarchy.
Simple situation built from other questions of mine:
public class Main extends MovieClip { // Document Class at Flash
private var objectA:ClassA;
private var objectB:ClassB;
public function Main() {
objectA = new ClassA();
addChild(objectA);
objectB = new ClassB();
addChild(objectB);
}
}
public class ClassA extends Sprite {
public function ClassA() {
addChild(new Bitmap(new BitmapData(20, 20, false, 0))); // A black square
addEventListener(MouseEvent.CLICK, clickedA);
}
public function clickedA(evt:MouseEvent):void {
dispatchEvent(new TextEvent("myEventType", true, false, "success"));
}
}
public class ClassB extends Sprite {
public function ClassB() {
addEventListener("myEventType", myFunction);
}
public function myFunction(evt:TextEvent):void {
trace(evt.text);
var color:uint = evt.text == "success" ? 0x00FF00 : 0xFF0000;
addChild(new Bitmap(new BitmapData(20, 20, false, color)));
}
}
myFunction
should react to the dispatch through Main
.
What do I have to implement on Main
to make it serve as a relay for dispatched events from a child to its other children?
Consider it can have many children as dispatchers and receivers/listeners. In other words, it has to be generic so I won't have to implement one relay for every case.