0

I'm having difficulty with the last piece in the puzzle on AS3 events.

I understand target classes inherit from EventDispatch or implement IEventDispatch and can register (amongst other methods) event listeners.

However what do the target classes register with? If an Event happens, how does AS3 know to pass the Event to the target classes?

Regards, shwell.

4

5 回答 5

0

Read this article about event phases and it will make more sense:

http://livedocs.adobe.com/flex/3/html/help.html?content=events_02.html

Hope this helps. Have a great day.

于 2013-07-31T08:51:02.467 回答
0

You can look at how starling event works

starling even dispatcher

When a displayObject bubbles an event, it will check if the parent of the displayObject exist and add the parent to bubbleList if exist util the ancestor of displayObject is null.

The following code is in starling eventDispatcher

var element:DisplayObject = this as DisplayObject;
var chain:Vector.<EventDispatcher> = new <EventDispatcher>[element];

while ((element = element.parent) != null)
    chain[int(length++)] = element;
于 2013-07-31T08:56:33.093 回答
0

In AS3, EventDispatcher is an implementation of the observer design pattern. This class implement the addEventLister, removeEventListener, dispatchEvent' andhasEventListener` methods. Internally, it also maintains a dictionary or similar data structure that contains the events which are currently being listened for, and a list of methods which have to be called when the event is dispatched. Something like this -

{"event1": [method7, method5, method3], "event2": [method3, method2], "event3": [method1]};

When addEventListener is called on an object, it creates a new key for the event in question and adds the method reference to its associated value list.

When dispatchEvent is called on the class, it fetches all the methods associated with the event and calls the methods attached with it. Each method is called with an instance of the Event class or its subclasses.

Removing an event listener obviously does the opposite of what adding does.

于 2013-07-31T08:59:19.390 回答
0

I guess you're missing of addEventListener() mechanics. This thing has a global side effect on event engine, registering a callback function along with caller this value to provide correct context of a fired event, with possible update of event.localX and event.localY properties by calling globalToLocal() either statically or dynamically, as the event bubbles up and down.

If you are, like me, confused about how does Flash player determine the target of an event - there is an internal "focus" pointer that determines which component of the SWF has keyboard focus, and that one is used to target keyboard events. For mouse events, most likely Flash engine calls getObjectsUnderPoint() to query for topmost IEventDispatcher compatible objects (not all of the DisplayObjects can process events), and that one is sent a mouse event, with the previous event's target to receive a say MouseEvent.ROLL_OUT or MouseEvent.MOUSE_OUT if the target has been changed. For other events, most likely the entire display list can react.

于 2013-07-31T09:06:30.333 回答
0

For objects in the display list, the following excerpt from Adobe is the answer "When Adobe® Flash® Player dispatches an Event object, that Event object makes a roundtrip journey from the root of the display list to the target node, checking each node for registered listeners.".

For non display objects, AS3 run time maintains a dictionary of all AS3 events containing bound variables. The bound variables are a reference to the event listeners.

于 2013-07-31T22:10:43.723 回答