views:

63

answers:

3

How does one check if mouse is over a symbol instance using ActionScript 3 / Flash CS5?

+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
Accepted your answer, but see my answer as well for details...
Yarin
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
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
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
@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