Detailed description of the addeventlistener () method in flex3 ()

Source: Internet
Author: User

Method details:

Source: Flash. Events. eventdispatcher class

Addeventlistener () method

Public Function addeventlistener (type: String, listener: function, usecapture: Boolean = false, priority: Int = 0, useweakreference: Boolean = false): void

Language Version: ActionScript 3.0
Runtimeversions: air 1.0, Flash Player 9

 

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 a listener whose usecapture is set to true, the listener only listens in the capture phase. If you use the same listener object to call addeventlistener () again and set usecapture to false, you will have two separate listeners: one for listening during the capture phase, the other listens 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 attached 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 ).

Parameter Analysis:

1. Type: String-event type.

2. Listener: function-listener function for event processing.
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.

3. usecapture: Boolean (default = false)-determine whether the listener is running in the capture, target, or bubble phase.
If usecapture is set to true, the listener only processes events in the capture phase, rather than events in the target or bubble phase.
If usecapture is set to false, the listener only processes events in the target or bubble phase. To listen for events in all three phases, call addeventlistener twice: Set usecapture to true at a time, and set usecapture to false at a time.

4. Priority: int (default = 0)-priority of the event listener.
The priority is specified by a signed 32-bit integer. The greater the number, the higher the priority. All listeners with a priority of N are processed before the listeners with a priority of n-1. If two or more listeners share the same priority, they are processed in the order they are added. The default priority is 0.

5. useweakreference: Boolean (default = false)-(whether it is a weak reference) determines whether the reference to the listener is a strong reference or a weak reference.
Strong reference (default value) prevents your listener from being recycled as garbage. Weak references do not have this function.
Class-level member functions are not garbage collection objects. Therefore, you can set useweakreference to true for class-level member functions without affecting them by garbage collection. If useweakreference is set to true for the listener that acts as a nested internal function, this function is used as a garbage collection function 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.

 

Raise argumenterror-the specified listener is not a function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.