Flash 中的 MovieClip MOUSE_OVER 问题

发布于 2024-11-29 18:52:56 字数 473 浏览 0 评论 0原文

我正在 Flash CS5 中构建一个小型应用程序,但遇到了问题。我已将设计师创建的相当复杂的 Adob​​e Illustrator 文件导入到我的应用程序中。该文件充满了我可以在 Flash 内部操作的各种图像、文本和其他组件。

我的应用程序中有一个组件,由一组包含一个顶部带有文本的基本正方形组成,我想将其设为热点,用户可以将鼠标悬停在该热点上并触发工具提示来显示。我已将整个组转换为 MovieClip,这将触发工具提示显示在 MOUSE_OVER 上。它唯一的瓶颈是用户触发它的时候。

由于某种原因,每当我将指针移到 MovieClip 的不同区域时,都会多次调用 MOUSE_OVER 事件。例如,将鼠标悬停在背景上会触发一次,然后将鼠标悬停在文本的不同区域上会触发多次,即使所有这些组件都组合在一个 MovieClip 内。

如何使这些对象表现为一个 MovieClip,以便将鼠标悬停在 MovieClip 的任何区域上只会触发 MOUSE_OVER 事件侦听器一次?

I am building a small application in Flash CS5, and I have run into a problem. I have imported a rather complex Adobe Illustrator file, created by a designer, into my application. This file is full of all kinds of images, text, and other components which I can manipulate inside of Flash.

There is one component in my application, consisting of a group containing a basic square with text on top of it, that I would like to make into a hotspot, which a user can mouse over and trigger a tooltip to display. I have converted this entire group into a MovieClip, which will trigger the tooltip to display on MOUSE_OVER. Its only choking point is when the user triggers it.

For some reason, whenever I move my pointer over different areas of the MovieClip, the MOUSE_OVER event is called several times. For example, mousing over the background fires it once, then mousing over different areas of the text will fire it several times, even though all of these components are grouped together inside of one MovieClip.

How can I cause these objects to behave as one MovieClip, so that mousing over any area of the MovieClip will only fire the MOUSE_OVER event listener once?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小瓶盖 2024-12-06 18:52:57

附带说明一下,这是由事件冒泡引起的。我喜欢自定义 MOUSE_OVER 事件来防止这种情况发生。

As a side note this is caused by the event bubbling. I am a fan of custom MOUSE_OVER events to prevent this.

回忆躺在深渊里 2024-12-06 18:52:56

实际上,您可以使用 ROLL_OVER 来完成相同的操作,而不需要前景剪辑。如果您已经将项目分组到 MovieClip 中,则只需为 MouseEvent.ROLL_OVER 添加监听器即可。它不仅会按照您想要的方式工作,而且还意味着额外的前景剪辑会减少混乱,因为我个人总是喜欢尽可能干净的代码(如果可能的话)。

movieclip.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler);

function onRollOverHandler(e:MouseEvent):void
{
    // this will run once when you move your mouse over the movieclip
}

然后,当鼠标移出 MovieClip 时,只需处理与该函数相反的函数即可。

movieclip.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler);

function onRollOutHandler(e:MouseEvent):void
{
    // this will run once when you move your mouse off of the movieclip
}

You can actually just use ROLL_OVER which does the same thing without the need of a foreground clip. If you have already grouped your items into a MovieClip then simply add a listener for MouseEvent.ROLL_OVER. Not only will it work just as you'd like but it means less clutter from the extra foreground clip as I personally always prefer as clean code as possible, if possible.

movieclip.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler);

function onRollOverHandler(e:MouseEvent):void
{
    // this will run once when you move your mouse over the movieclip
}

Then simply have the opposite of this function to handle when the mouse moves off of the MovieClip.

movieclip.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler);

function onRollOutHandler(e:MouseEvent):void
{
    // this will run once when you move your mouse off of the movieclip
}
旧城空念 2024-12-06 18:52:56

如果您的 MovieClip 中的对象不需要处于活动状态,您可以使用

yourMC.mouseChildren=false;
yourMC.addEventListener(MouseEvent.MOUSE_OVER,yourOverHandlerFunction);

If the objects within your MovieClip do not need to be active, you could use

yourMC.mouseChildren=false;
yourMC.addEventListener(MouseEvent.MOUSE_OVER,yourOverHandlerFunction);
£噩梦荏苒 2024-12-06 18:52:56

问题是 MOUSE_OVER 是由每个项目触发的——这是预期的,每个项目都会本机触发该事件,并且所有事件都会冒泡。查看事件冒泡以找到更多相关信息。

有几种方法可以解决这个问题:

  1. 创建一个前景剪辑并收听它。在我看来,这是迄今为止最好的选择。
  2. 您可以使用 useCapture 参数,根据我的经验,该参数可以解决许多此类问题。您可以调用 addEventListener(name,callback, true),而不是 addEventListener(name,callback)removeEventListener 也是如此。
  3. 手动事件跟踪!这是最后手段,它提供了最大的可靠性,但代价是最差的性能和最令人头痛的问题。这或多或少涉及全局事件侦听器和命中测试。仅在绝望时使用。

The problem is that MOUSE_OVER is being fired by each of the items -- and that is expected, each of them will fire that event natively and the events all bubble. Look up event bubbling to find more on that.

There are a couple of ways to work around this:

  1. Create a foreground clip and listen to that instead. This is, in my opinion, by far, the best option.
  2. You could use the useCapture parameter which, in my experience, will fix many of these problems. Instead of addEventListener(name, callback) you would call addEventListener(name, callback, true). This is also true of removeEventListener.
  3. Manual event tracking! A weapon of last resort, this affords the most reliability at the expense of the worst performance and the biggest headache. This would involve a more-or-less global event listener and hit tests. Only use if desperate.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文