开发者

How to determine the display object hierarchy an event will capture/bubble through?

I开发者_Go百科n Actionscript 3 is there any way to determine the precise display object hierarchy an event will traverse through when dispatched from a given object? The object is not necessarily a display object.

Although I could imagine this actually being useful somehow, in my particular case it is a learning exercise. I'm trying to master the event system in flash and have some locations where I'm very curious what path an event will take when I dispatch it.


There is absolutely a way. Flash has a few convenient functions/properties that could facilitate this.

Because all flash objects that extend** another class, like DisplayObject, have a parent property, you can simply write:

import "flash.utils.getQualifiedClassName";

var bubblePath:String = "";

var parentObject:* = parent;
while( parentObject ) {
    bubblePath += getQualifiedClassName( parentObject ) + "\n";
    parentObject = parentObject.parent;
}

trace( bubblePath );

This will give you a string with the names of all the classes. I am not sure if that is what you are looking for in your experiment, but it shouldn't be difficult to modify output as necessary.


The answer is sort of. Events aren't limited to DisplayObjects, and when they are triggered by DisplayObjects, they don't always follow the same pattern -- the Event may very based on what type of DisplayObject and whether the event was triggered programmatically (through dispatchEvent) or natively (by a mouseClick). It may also vary based on whether bubbling is enabled or not and if it is a custom event. A Loader's complete event, for example, does not make sense to propagate to parent objects, even though Loader is a display object.

If you're trying to figure out the parents of the object which dispatched an event, then you'll want something like this:

import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
myDispatcher.addEventListener( MouseEvent.CLICK, clickHandler );

function clickHandler( event:MouseEvent ):void
{
    // event.currentTarget is the object which you're listening to.
    // event.target is the object which actually dispatched the event
    // (if you're listening to a MovieClip and a button inside it is clicked,
    // target will refer to the button, currentTarget the MovieClip)
    var ancestry:Vector.<DisplayObjectContainer> = getAncestry( event.currentTarget as DisplayObject );
    // do somethign with all of the parents.

}

// this function takes a DisplayObject and returns all of its ancestor objects 
// all the way to the root as a Vector.
function getAncestry( obj:DisplayObject ):Vector.<DisplayObjectContainer>
{
    var par:Object = obj.parent;
    var ancestry:Vector.<DisplayObjectContainer> = new Vector.<DisplayObjectContainer>();
    while( par )
    {
        par = par.parent;
        ancestry.push( par as DisplayObject );
        if( par == root ) break;//sometimes the root has a parent.
    }
    return ancestry;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜