How does one check if mouse is over a symbol instance using ActionScript 3 / Flash CS5?
views:
63answers:
3
+4
A:
Check out the getObjectsUnderPoint method if you want to get a list of all the objects that are children of a display object container.
Otherwise, you could use hitTestPoint and pass a point with the mouseX and mouseY coordinates.
Juan Pablo Califano
2010-09-27 23:49:46
Accepted your answer, but see my answer as well for details...
Yarin
2010-09-29 21:37:52
A:
Why not use a MouseEvent?
symbol.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
function onMouseOver(evt:MouseEvent):void
{
//is called when mouse is over your symbol.
}
Tobias Kun
2010-09-28 06:21:26
Tobias, this is only a feasable solution if your component is uncovered and ignoring any subcomponents. For multi-layer layouts this mostly doesn't cut it.
Yarin
2010-09-29 21:39:55
A:
Juan Pablo is correct, but I've found that hitTestPoint can be finicky if not applied correctly. Specifically, the third argument (shapeFlag Boolean) should be TRUE (Default is FALSE) and using event.stageX/Y on mouse events often works when mouseX/Y does not.
Can't explain why exactly, but the following is pretty fool proof in my experience:
if (hitTestPoint(event.stageX, event.stageY, true))
// Do something
Yarin
2010-09-29 21:36:47
@Yarin. Good points. I neglected to mention it, but as you've found out, shapeFlag = `true` is almost always what you want (despite the default being `false`), because otherwise the bounding box of the objects is tested, not the shape (if you have a bitmap, though, it makes no difference). Also, `hitTestPoint` takes global coordinates, that's why you have to either take a point and convert it to the global coordinate space (read: `obj.parent.localToGlobal(obj.x,obj.y)`) or just take advantage of the `stageX` and `stageY` properties of the `MouseEvent`.
Juan Pablo Califano
2010-09-30 01:16:35