AS3 - Button inside MovieClip triggers MC's event

大憨熊 提交于 2019-12-02 00:22:58

There is no way to prevent bubbling except setting the bubbles parameter as false in the dispatchEvent.

dispatchEvent(EVENT_TYPE, BUBBLES,....);

However, you can avoid the bubbling by doing a check. Just have the below line as first line of your listener function it avoids the events dispatched from all objects other then targets.

if(e.eventPhase != EventPhase.AT_TARGET) return;

So, for your sample code, when you click on the button both the events dispatches but in the mcDown function it won't execute after the above said line.

If you add button in a MC, and you click on the button, you also click on the MC, because the part of the MC that is under the button is still there and it runes the function for the whole of the MC, you can't remove it.

So it's good idea to make a function that will check if the button is pressed, otherwise it will run the function for the whole of the MC.

This one should do it.

//add this in you constructor

mc.addEventListener(MouseEvent.MOUSE_DOWN, myReleaseFunc);
function myReleaseFunc(e:MouseEvent):void {
    if(e.currentTarget.name == Btn1) //Btn1 is instance name for a button
    {
         Btn_func1();
    }
    else if(e.currentTarget.name == Btn2) //Btn2 is another button.
    {
         Btn_func2();
         //For every button you'll need to add another function and if statement to check if that button was clicked.
    }
    else
    {
         Mc_func();
    }

} 

// this outside the main class

function Mc_func():void{
    //you code here
}
function Btn_func1():void{
    //you code here
}
function Btn_func2():void{
    //you code here
}

I think that this way is much more effiecient and it will works better and faster and you'll have a lot smaller chance of overloading the system.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!