Flash 中的 MovieClip MOUSE_OVER 问题
我正在 Flash CS5 中构建一个小型应用程序,但遇到了问题。我已将设计师创建的相当复杂的 Adobe 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
附带说明一下,这是由事件冒泡引起的。我喜欢自定义 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.
实际上,您可以使用
ROLL_OVER
来完成相同的操作,而不需要前景剪辑。如果您已经将项目分组到 MovieClip 中,则只需为MouseEvent.ROLL_OVER
添加监听器即可。它不仅会按照您想要的方式工作,而且还意味着额外的前景剪辑会减少混乱,因为我个人总是喜欢尽可能干净的代码(如果可能的话)。然后,当鼠标移出 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 forMouseEvent.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.Then simply have the opposite of this function to handle when the mouse moves off of the MovieClip.
如果您的 MovieClip 中的对象不需要处于活动状态,您可以使用
If the objects within your MovieClip do not need to be active, you could use
问题是 MOUSE_OVER 是由每个项目触发的——这是预期的,每个项目都会本机触发该事件,并且所有事件都会冒泡。查看事件冒泡以找到更多相关信息。
有几种方法可以解决这个问题:
useCapture
参数,根据我的经验,该参数可以解决许多此类问题。您可以调用addEventListener(name,callback, true)
,而不是addEventListener(name,callback)
。removeEventListener
也是如此。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:
useCapture
parameter which, in my experience, will fix many of these problems. Instead ofaddEventListener(name, callback)
you would calladdEventListener(name, callback, true)
. This is also true ofremoveEventListener
.