Is it possible to send a custom event to a child?
testcase:
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
public class AS3Test extends Sprite
{
public var a:A;
public function AS3Test()
{
this.a = new A();
this.a.addEventListener("E", function(e:E):void{trace('got event');});
this.addChild(this.a);
this.addEventListener(MouseEvent.CLICK, this.click);
}
private function click(e:MouseEvent):void {
trace('click sends event');
this.dispatchEvent(new E());
}
}
}
import flash.display.Sprite;
import flash.events.Event;
import flash.display.Shape;
import flash.display.Graphics;
internal class A extends Sprite {
public function A() {
super();
var rect:Shape = new Shape();
var g:Graphics = rect.graphics;
g.lineStyle(1,0xffffff);
g.beginFill(0x000000);
g.drawRect(0,0,100,100);
this.addChild(rect);
}
}
internal class E extends Event {
public function E() {
super('E', true, true);
}
}
when I click the parent, it gets click event.
in the click event handler, the parent dispatches a custom event of type E.
and the child is listening for E.
obj.dispatchEvent() by default targets for obj it is called from.
So, the event being dispatched won’t reach the child if obj had one.
Is there a way to send an event from parent to child?
Better yet, is it possible to send an event to an object not in the DisplayList nor is a DisplayObject (the object is instanciated, but it doesn’t have graphical role in the program)?
Thank you.