as3 mouseEnabled still a problem for me
Asked Answered
C

2

10

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???

Clump answered 11/2, 2010 at 22:47 Comment(0)
D
28

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 :)

Dement answered 21/4, 2010 at 20:58 Comment(1)
Thanks, I was having a similar issue with textfields, and it was in a container which shared a common ancestor to a button. With no direct reference to the container I was able to call the parent and set the mouseEnabled/Children to false.Tampa
C
4

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.

Contemptuous answered 11/2, 2010 at 22:53 Comment(2)
ok, forget the "many objects" part. Just one object that I want disabled, and one that I want enabled. The enabled object is very small and not affecting this. Here's what I get when I use the code you posted: instance5 flash.display::SpriteClump
ok, so there's a sprite on there without an instance name that is receiving the click instead of cloud. does your tree have an instance name? I'd give your clips names so you can figure out which one is actually blocking.Contemptuous

© 2022 - 2024 — McMap. All rights reserved.