开发者

MovieClip in Flash MOUSE_OVER Issues

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?


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
}


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

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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜