Nested MouseEvent Handlers (How to Prevent Event Bubbling?)

Hi All,

I seem to have been stumped by what I assume is a very simple (and common) problem. I have attached a simplified version of the problem as a .fla but will continue to desribe it here.

I have a “parent” button which itself contains child “action” buttons. The problem is, when the user clicks an “action” button, the event bubbles up through the Display List and reaches the the “parent” button resulting in both MouseEvent.CLICK Event Handlers being triggered. I simply want a way to stop the event bubbling past the action buttons.

Here’s the sample code for the problem:


parentButton.buttonMode = true;
var buttonContainer : EventDispatcher = EventDispatcher(parentButton["buttonContainer"]);

// Bind EventHandler to parentButton to catch click events on the 
// "Main" button.
parentButton.addEventListener(MouseEvent.CLICK, this.handleParentButtonClicked);

// Bind EventHandlers for the two "Action" buttons.
buttonContainer.addEventListener(MouseEvent.CLICK, this.handleActionButtonClicked);


// "Main" ParentButton has been clicked.
function handleParentButtonClicked(event : MouseEvent) : void
{
    trace("Parent Button Clicked.");
}

// One of the ActionButtons has been clicked.
function handleActionButtonClicked(event : MouseEvent) : void
{
    // Here's the problem, the event will bubble down to the
    // handleParentButtonClicked handler.
    trace("Action Button Clicked.");
}

I have tried most thing, comparison of event.target and event.currentTarget don’t appear to yeild anything usable (as the parentButton itself has children from which the event can originate). I have “solved” the problem by modifying the handleParentButtonClicked eventHandler as follows:


 [LEFT]function handleParentButtonClicked(event : MouseEvent) : void
{
    var buttonContainer : DisplayObjectContainer = parentButton["buttonContainer"];
    if (!buttonContainer.contains(DisplayObjectContainer(event.target)))
    {
        trace("Parent Button Clicked.");
    }
}

[/LEFT]
However, I don’t consider this approach very elegant and was hoping that there was a neater solution.

Thanks
jonny.