开发者

AS3 Concept similar to Event-Bubbling, in non-display-objects?

开发者 https://www.devze.com 2023-03-31 12:35 出处:网络
Events in Flash/AS3 are very much linked to the display list. There is the capture, target, and bubbling phase开发者_如何转开发, which is great when it comes to objects visible on the stage.

Events in Flash/AS3 are very much linked to the display list. There is the capture, target, and bubbling phase开发者_如何转开发, which is great when it comes to objects visible on the stage.

But is there a similar concept for non display objects, outside of the display list?

If we have objects A, B and C, where C was created in object B, and B was created in object A, and none of them are display objects: How can A listen for things happening in C?


You can solve your problem using at least two strategies:

1) Redispatching of events. Say C dispatches some event. In B we subscribe this event and redispatch it:

var c:C = new C();
c.addEventListener("myEvent", myEventHandler);

private function myEventHandler (event:Event):void
{
    dispatchEvent(event);
}

Remember you should implement clone() properly for your custom event class in this case.

You also can translate event from C to some other event in B and dispatch it.

2) Pass C to A. You can do it using interface flash.events.IEventDispatcher.

Something like the following:

In B:

private var _c:IEventDispatcher;

public function get innerInstance():IEventDispatcher
{
    return _c;
}

public function B()
{
    _c = new C();
}

In A:

var b:B = new B();
b.innerInstance.addEventListener("myEvent", myEventHandler);
0

精彩评论

暂无评论...
验证码 换一张
取 消