Use event bubbling via AS2.0

Source: Internet
Author: User
Ubb

Event bubbling (incident float mechanism)
OK, let me call it the event bubbling. Originally Ralf published in his blog on the As2.0 use event bubbling method. It's a creative idea.
Event bubbling was originally only used in AS3.0. In the event bubbling mechanism, the object that generated the event first receives the event. The event then propagates up according to the hierarchy of the object. So the simple use of the event bubbling in Flash is a lot simpler for the MC that handles nesting. The use of event bubbling is a good reinforcement of the continuity of incident handling.
Ralf's solution is to replace the dispatch method in its own way, the following is an example of Ralf, and you can modify it yourself:

public static initializebubbling (Dispatcher:movieclip) {
var parentdispatcher = dispatcher._parent; Add traversal Optionally, comments below
var olddispatchevent = dispatcher.dispatchevent;
Dispatcher.dispatchevent = function (evt) {
Olddispatchevent.call (Dispatcher, evt);
if (evt.bubbles) {
Parentdispatcher.dispatchevent (EVT);
}
}
}

More detailed articles, I think it is better to read the original text
At the same time, Peter Elst also wrote an example to use.
For example, when you do not use event bubbling, you need to make MC1.MC2 broadcast events in the following ways.

Mc1.mc2.addEventListener ("Someevent", Someeventhandler);
You can do this if you use event bubbling.
Mc1.addeventlistener ("Someevent", Someeventhandler);
Because of this event chain mechanism, you don't have to worry about where the time is coming from.
Import Mx.events.EventDispatcher;
Ralf Bokelberg ' s AS2.0 event bubbling Workaround
initializebubbling = function (dispatcher:movieclip) {
var parentdispatcher = dispatcher._parent;
var olddispatchevent = dispatcher.dispatchevent;
Dispatcher.dispatchevent = function (evt) {
Olddispatchevent.call (Dispatcher, evt);
if (evt.bubbles) {
Parentdispatcher.dispatchevent (EVT);
}
}
}
Create Empty MovieClips
var mc1:movieclip = Createemptymovieclip ("MC1", 1);
var mc2:movieclip = Mc1.createemptymovieclip ("MC2", 1);
Initialize Eventdispatcher
Eventdispatcher.initialize (MC1);
Eventdispatcher.initialize (MC1.MC2);
Initialize event bubbling
Initializebubbling (MC1.MC2);
Add event listeners to MovieClips
Mc1.mc2.addEventListener ("Customevent", function () {
Trace ("Customevent Captured by MC1.MC2");
});
Mc1.addeventlistener ("Customevent", function () {
Trace ("Customevent Captured by MC1");
});
Dispatch Event
Mc1.mc2.dispatchEvent ({type: "Customevent", bubbles:true});

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.