AS3 - 按钮内部的MovieClip触发MC的事件 [英] AS3 - Button inside MovieClip triggers MC's event

查看:155
本文介绍了AS3 - 按钮内部的MovieClip触发MC的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在舞台上,我有一个名为MC里面画一个简单的矩形影片剪辑。 MC还有一个名为BTN这是另一种简单的矩形(比三菱商事的长方形小明明)的按钮,孩子。然后,我在舞台上这个code。

On stage, I have a MovieClip named "mc" with a simple rectangle drawn inside. mc also has a Button child named "btn" which is another simple rectangle ( smaller than mc's rectangle obviously). Then I have this code on stage.

function mcDown( _e:MouseEvent):void{
    trace( "mc" );
}
function btnClick( _e:MouseEvent):void{
    trace( "btn" );
}
mc.addEventListener( MouseEvent.MOUSE_DOWN, mcDown );
mc.btn.addEventListener( MouseEvent.CLICK, btnClick );

我遇到的问题是,当单击该按钮,mcDown事件也触发和跟踪两个三菱商事和BTN。

The problem I am having is when click the button, mcDown event is also triggered and traces both "mc" and "btn".

我怎样才能让这个当我按一下按钮,它只会触发btnClick而不是沿着mcDown?我想,而不是点击,同样的问题MOUSE_UP。而mcDown事件必须保持MOUSE_DOWN。

How can I make it so that when I click the button, it triggers only btnClick and not mcDown along? I tried MOUSE_UP instead of CLICK, same problem. And mcDown event has to remain MOUSE_DOWN.

推荐答案

有没有办法为prevent冒泡除了设置气泡参数中则dispatchEvent为假。

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;

所以,为您的样品code,当你点击按钮,这两个事件的调度,但在 mcDown 功能后上面说的不会执行行。

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.

这篇关于AS3 - 按钮内部的MovieClip触发MC的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆