The event listener must also inheritEventdispatcherClass, so that you can useAddeventlistenerMethod:
Public Function addeventlistener (type: String, listener: function, usecapture: Boolean = False , Priority: Int = 0 , Useweakreference: Boolean = False ): Void
Use the eventdispatcher object to register an event listener object so that the listener can receive Event Notifications. You can register an event listener on all nodes in the display list for specific types of events, phases, and priorities.
After an event listener is successfully registered, you cannot change its priority by calling addeventlistener. To change the priority of the listener, you must first call removelistener (). Then, you can use the new priority to register the listener again.
Remember, after registering this listener, if you continue to call addeventlistener () with different types or usecapture values, a separate listener registration will be created. For example, if you first register usecapture True , The listener only listens in the capture phase. If you use the same listener object to call addeventlistener () Again, and set usecapture False Then there will be two separate listeners: one for listening in the capture phase, and the other for listening in the target and bubble phases.
You cannot register an event listener only for the target or bubble phase. These stages appear in pairs during registration, because the bubble stage is only applicable to the ancestor of the target node.
If you no longer need an event listener, you can call removeeventlistener () to delete it. Otherwise, memory problems may occur. Because the garbage collector does not delete objects that still contain references, it does not automatically delete objects that use registered event listeners from the memory.
When you copy an eventdispatcher instance, the added event listener is not copied. (If an event listener is required for a newly created node, the listener must be appended after the node is created ). However, if you move the eventdispatcher instance, the added event listener will also move.
If you register an event listener on the node that is processing the event, the event listener will not be triggered in the current phase, but will be triggered later in the event stream, such as the bubble stage.
If you delete the event listener from the node that is processing the event, the event listener is still triggered by the current operation. After the event listener is deleted, it will never be called again (unless it is registered for future processing ).
Parameters
Type: String-event type.
Listener: function-the listener function used to process events. This function must accept the event object as its unique parameter and cannot return any results, as shown in the following example:
Function (EVT: Event ): Void
The function can have any name.
Usecapture: Boolean ( Default = False )-Determine whether the listener is running in the capture, target, or bubble phase. If you set usecapture True The listener only processes events in the capture phase, instead of events in the target or bubble phase. If usecapture is False The listener only processes events in the target or bubble phase. To listen for events in all three phases, call addeventlistener twice and set usecapture True , Set usecapture False .
Priority: Int ( Default = 0 )-Priority of the event listener. The priority is determined by a signed 32 Specifies an integer. The greater the number, the higher the priority. All listeners whose priority is n will - 1 . If two or more listeners share the same priority, they are processed in the order they are added. The default priority is 0 .
Useweakreference: Boolean ( Default = False )-Determine whether the listener references strong or weak references. Strong reference (default value) prevents your listener from being recycled as garbage. Weak references do not have this function.
The class-level member function does not belong to the garbage collection object. Therefore, you can set useweakreference True They will not be affected by garbage collection. If you set useweakreference True , The function is recycled as garbage and is no longer a permanent function. If you create a reference to the internal function (Save the function to another variable), the function will not be collected as garbage and will remain permanently.