Mike T. Henderson

Interactive Design & Art Direction

MovieClip.hitTest() coordinate issues

Oct 9 2007

I came across this issue yesterday in Flash with trying to use hitTest() and detect when my mouse intersected with a certain movieClip. The problem came with me trying to do this while using a liquid Flash layout. When I would publish the file it would work fine, but once I resized the window, Flash would still think that my movieClip was in the lower left corner of the stage. For example, when I moused over the clip, nothing would return, but if I would move the mouse about 150 pixels below, then it would.

I searched, and searched, and searched for over 5 hours last night for an answer. Finally, I came across the solution on someone's personal blog. Thankfully, it ended up being an easy fix.

Basically, the problem lies with how the hitTest() uses the Stage's coordinate system. The solution was to us localToGlobal() to redefine the _xmouse/_ymouse.

Solution:

var point:Object = {x:_xmouse, y:_ymouse}
localToGlobal(point);
myMC.hitTest(point.x, point.y)  // instead of myMC.hitTest(_xmouse, _ymouse);

2 Posted under: Collision

2 Responses

  1. Vengeance says:

    If you use _root._xmouse and _root._ymouse it fixes the problem. See, when you refer to movieclip._xmouse, it uses movieclip._x-_root._xmouse, so, if the mouse is at the registration point of the movieclip, movieclip._xmouse=0.
    simply use
    myMC.hitTest(_root._xmouse, _root._ymouse);

  2. Yeah, in most cases that is the best fix. However, I wasn't lucky enough to get that to work in my instance.

    It's been a few months since I came across this, but if I remember correctly, the reason I was having trouble with the x/y coordinates was because I was working with a liquid layout and while I was aligning my site to the top left, and centering my content in the middle horizontally and vertically, Flash seemed to be getting confused between the two.

    I tried checking for the root._xmouse/_ymouse, but it too, was giving me the wrong coordinates. I can't quite remember all the details(I probably should have explained better while the memory was still fresh), but using the localToGlobal object was the only method that worked. I might have to do some more testing on this to figure out exactly what was going on. I have read that if you can avoid using localToGlobal/globalToLocal, it makes for more optimized code.

Leave a Reply