问题
A couple years in now, there's still something about mouseEnabled I'm not getting. I have a Sprite (for example here "Sky", that contains many objects, one of them is a Cloud, which I do not want to receive Mouse Events. I overlay this Sky on some other display objects. I want the cloud to be visible, but not to block mouse events. If you see a tree through the clouds you should be able to click on the tree.
In the Sky class:
mouseEnabled = false;
cloud.mouseEnabled = false;
cloud.mouseChildren = false;
Even with this configuration, when the cloud is over the tree I can't click on the tree because the cloud blocks it. Why???
回答1:
Even though Sky has mouseEnabled/mouseChildren set to false... it's still an object, it still takes up space, and therefore still acts as a hit area for any PARENT containers that don't have mouseEnabled/mouseChildren set to false.
Therefore, I suspect your Sky object is not in the same parent container as your Tree object. Your Sky object probably has its own parent container object, which is the culprit intercepting the events.
To elaborate: Any object that contains ANYTHING will have a hit area and will intercept mouse clicks, even though all the individual things it contains (shapes, child objects, etc.) may have mouseEnabled/mouseChildren set to false.
So even though your Sky object has mouseEnabled set to false, your Sky (and it's children) still take up space, and therefore still give Sky's parent container a hit area to intercept mouse events.
Your solution, therefore, is to make sure all the parent containers of Sky have thier mouseEnabled property set to false, at least up to (but not including) the first common ancestor container of the Tree and Sky objects.
Also, by setting mouseEnabled=false and leaving mouseChildren=true, you can have a container where only select children with mouseEnabled=true receive click events :)
回答2:
You say there's "many objects" in there? More than likely something else is blocking it. I recommend adding a listener to the stage and then you can see which object is receiving clicks:
import flash.utils.getQualifiedClassName;
stage.addEventListener(MouseEvent.CLICK, onClick);
private function onClick(event:MouseEvent):void
{
trace(event.target.name, getQualifiedClassName(event.target));
}
Post more code and we can probably help more.
来源:https://stackoverflow.com/questions/2248381/as3-mouseenabled-still-a-problem-for-me